ExeclUtil.java
资源名称:shihua.rar [点击查看]
上传用户:zghglow
上传日期:2022-08-09
资源大小:27227k
文件大小:3k
源码类别:
WEB源码(ASP,PHP,...)
开发平台:
JavaScript
- package com.chinacannel.common;
- import java.util.List;
- import java.io.InputStream;
- import java.util.ArrayList;
- import jxl.Workbook;
- import jxl.Sheet;
- import jxl.Cell;
- import org.apache.commons.logging.LogFactory;
- import org.apache.commons.logging.Log;
- import java.io.OutputStream;
- import java.util.*;
- import java.io.FileOutputStream;
- public class ExeclUtil {
- private static Log log = LogFactory.getLog(ExeclUtil.class);
- public ExeclUtil() {
- }
- public List readFromExcel(InputStream is) {
- int[] step=new int[]{0,2,3,4,6,10,23,42};
- Workbook wb = null;
- List rowsList = new ArrayList();
- try {
- wb = Workbook.getWorkbook(is); // 得到工作薄
- Sheet[] sheets = wb.getSheets(); // 得到所有工作表
- for (int k = 0; k < sheets.length; k++) {
- Sheet st = sheets[k];
- String sheetName = st.getName();
- String year = "";
- int rows = st.getRows();
- int columns = st.getColumns();
- if (rows > 0) {
- year = st.getCell(0, 0).getContents().substring(0, 4);
- }
- for (int i = 1; i < rows; i++) {
- List rowList = new ArrayList();
- for (int j = 0; j < step.length; j++) {
- Cell cell1 = st.getCell(step[j], i); // 得到工作表的一个单元格
- String content = cell1.getContents(); // getContents()将Cell中的字符转为字符串
- rowList.add(content.trim());
- }
- if (rowList.size() > 0) {
- rowsList.add(rowList);
- }
- // rowsList.add(sheetName);
- }
- }
- return rowsList;
- } catch (Exception ex) {
- log.error(ex.getMessage(), ex);
- return null;
- } finally {
- try {
- wb.close(); // 关闭工作薄
- } catch (Exception ex2) {
- log.error(ex2.getMessage(), ex2);
- }
- }
- }
- public void outputExcel(OutputStream os,List list) throws
- Exception {
- // os = new FileOutputStream("c:\excel2.xls");
- // servletResponse.setContentType("application/octet-stream; charset=GBK");;
- // OutputStream os=servletResponse.getOutputStream();
- jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);
- jxl.write.WritableSheet ws = wwb.createSheet("Test Sheet 1", 0);
- if(list!=null){
- int i=0;
- Iterator iter = list.iterator();
- while (iter.hasNext()) {
- Object item = (Object) iter.next();
- if((item instanceof List)&&item!=null ){
- int j=0;
- Iterator iter2 = ((List)item).iterator();
- while (iter2.hasNext()) {
- String item2 = (String) iter2.next();
- jxl.write.Label label = new jxl.write.Label(j, i,item2);
- j++;
- ws.addCell(label);
- }
- }
- i++;
- }
- }
- // jxl.write.Label labelC = new jxl.write.Label(0, 0,
- // "This is a Label cell 1");
- // ws.addCell(labelC);
- wwb.write();
- wwb.close();
- }
- }