ExeclUtil.java
上传用户:zghglow
上传日期:2022-08-09
资源大小:27227k
文件大小:3k
源码类别:

WEB源码(ASP,PHP,...)

开发平台:

JavaScript

  1. package com.chinacannel.common;
  2. import java.util.List;
  3. import java.io.InputStream;
  4. import java.util.ArrayList;
  5. import jxl.Workbook;
  6. import jxl.Sheet;
  7. import jxl.Cell;
  8. import org.apache.commons.logging.LogFactory;
  9. import org.apache.commons.logging.Log;
  10. import java.io.OutputStream;
  11. import java.util.*;
  12. import java.io.FileOutputStream;
  13. public class ExeclUtil {
  14.       private static Log log = LogFactory.getLog(ExeclUtil.class);
  15.     public ExeclUtil() {
  16.     }
  17.     public List readFromExcel(InputStream is) {
  18.         int[] step=new int[]{0,2,3,4,6,10,23,42};
  19.         Workbook wb = null;
  20.         List rowsList = new ArrayList();
  21.         try {
  22.             wb = Workbook.getWorkbook(is); // 得到工作薄
  23.             Sheet[] sheets = wb.getSheets(); // 得到所有工作表
  24.             for (int k = 0; k < sheets.length; k++) {
  25.                 Sheet st = sheets[k];
  26.                 String sheetName = st.getName();
  27.                 String year = "";
  28.                 int rows = st.getRows();
  29.                 int columns = st.getColumns();
  30.                 if (rows > 0) {
  31.                     year = st.getCell(0, 0).getContents().substring(0, 4);
  32.                 }
  33.                 for (int i = 1; i < rows; i++) {
  34.                     List rowList = new ArrayList();
  35.                     for (int j = 0; j < step.length; j++) {
  36.                         Cell cell1 = st.getCell(step[j], i); // 得到工作表的一个单元格
  37.                         String content = cell1.getContents(); // getContents()将Cell中的字符转为字符串
  38.                         rowList.add(content.trim());
  39.                     }
  40.                     if (rowList.size() > 0) {
  41.                         rowsList.add(rowList);
  42.                     }
  43. //                    rowsList.add(sheetName);
  44.                 }
  45.             }
  46.             return rowsList;
  47.         } catch (Exception ex) {
  48.             log.error(ex.getMessage(), ex);
  49.             return null;
  50.         } finally {
  51.             try {
  52.                 wb.close(); // 关闭工作薄
  53.             } catch (Exception ex2) {
  54.                 log.error(ex2.getMessage(), ex2);
  55.             }
  56.         }
  57.     }
  58.     public void outputExcel(OutputStream os,List list) throws
  59.              Exception {
  60.  //       os = new FileOutputStream("c:\excel2.xls");
  61. //             servletResponse.setContentType("application/octet-stream; charset=GBK");;
  62. //         OutputStream os=servletResponse.getOutputStream();
  63.          jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);
  64.          jxl.write.WritableSheet ws = wwb.createSheet("Test Sheet 1", 0);
  65.          if(list!=null){
  66.              int i=0;
  67.              Iterator iter = list.iterator();
  68.              while (iter.hasNext()) {
  69.                  Object item = (Object) iter.next();
  70.                  if((item instanceof List)&&item!=null ){
  71.                      int j=0;
  72.                      Iterator iter2 = ((List)item).iterator();
  73.                      while (iter2.hasNext()) {
  74.                          String item2 = (String) iter2.next();
  75.                          jxl.write.Label label = new jxl.write.Label(j, i,item2);
  76.                          j++;
  77.                          ws.addCell(label);
  78.                      }
  79.                  }
  80.                  i++;
  81.              }
  82.          }
  83. //         jxl.write.Label labelC = new jxl.write.Label(0, 0,
  84. //                 "This is a Label cell 1");
  85. //         ws.addCell(labelC);
  86.          wwb.write();
  87.          wwb.close();
  88.     }
  89. }