package com.zhaochao.utils; import java.util.List; /** * * excel的行对象 * * @author 赵超 * */ public class ExcelRow { private List<String> colData; //当前行的所有列数据 public Integer getCols() { return colData.size(); } public List<String> getColData() { return colData; } public void setColData(List<String> colData2) { this.colData = colData2; } }
package com.zhaochao.utils; import java.util.List; /** * * Excel页对象 * * @author 赵超 * */ public class ExcelPage { private List<ExcelRow> rowData; //当页excel的所有行数据 public Integer getRows() { return rowData.size(); } public List<ExcelRow> getRowData() { return rowData; } public void setRowData(List<ExcelRow> rowData) { this.rowData = rowData; } }
package com.zhaochao.utils; import java.util.List; /** * * Excel模型类 * * * @author 赵超 * */ public class ExcelModel { private List<ExcelPage> pageData; //Excel每页的数据 public Integer getPages() { return pageData.size(); } public List<ExcelPage> getPageData() { return pageData; } public void setPageData(List<ExcelPage> pageData) { this.pageData = pageData; } }
package com.zhaochao.utils; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; import jxl.Workbook; import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; /** * * @author 操作excel工具类 * */ public class ExcelUtil { public static void main(String [] args){ //testWriteExcel(); testReadExcel(); } public static void testReadExcel(){ //String urlStr="http://baaifile.b0.upaiyun.com/uploadfile/2014/10/25/201410251038347176.xls"; //String urlStr="C:\\Users\\Administrator\\Desktop\\privateCoupon.xls"; String urlStr="c://user.xls"; ExcelModel model= ExcelUtil.ReadExcel(urlStr); int pageSize=0; for(ExcelPage page: model.getPageData()){ for(ExcelRow row:page.getRowData()){ for(String col:row.getColData()){ System.out.print(col+" "); } System.out.println(); } System.out.println("======总共"+model.getPages()+"页=====第"+(++pageSize)+"页===================="); } } public static void testWriteExcel(){ String fileName="c://user.xls"; //设置两列数据 List<String> cols=new ArrayList<String>(); cols.add("admin"); cols.add("123"); //设置行数据 ExcelRow row=new ExcelRow(); row.setColData(cols); List<ExcelRow> rows=new ArrayList<ExcelRow>(); rows.add(row); rows.add(row); //设置页数据 ExcelPage page=new ExcelPage(); page.setRowData(rows); List<ExcelPage> pages=new ArrayList<ExcelPage>(); pages.add(page); pages.add(page); //设置excleModel ExcelModel model=new ExcelModel(); model.setPageData(pages); writeExcel(fileName,model); } /** * 在线解析Excel * * @param urlStr 可以是http://开头的url,也可以是文件路径 * * @return 返回ExcelMode */ private static ExcelModel ReadExcel(String urlStr) { ExcelModel excle= new ExcelModel(); try { InputStream is=null; if(urlStr.startsWith("http://")){ URL url=new URL(urlStr); HttpURLConnection httpUrl=(HttpURLConnection) url.openConnection(); httpUrl.connect(); is = httpUrl.getInputStream(); }else{ File f=new File(urlStr); if(f.exists()){ is= new FileInputStream(f) ; }else{ throw new Exception("文件不存"); } } Workbook wb = Workbook.getWorkbook(is); jxl.Sheet [] sheet =wb.getSheets(); List<ExcelPage> pageDate=new ArrayList<ExcelPage>(); for(int s=0;s<sheet.length;s++){ ExcelPage page=new ExcelPage(); List<ExcelRow> rows=new ArrayList<ExcelRow>(); for(int i=0;i<sheet[s].getRows();i++){ ExcelRow row=new ExcelRow(); List<String> colData=new ArrayList<String>(); for(int j=0;j<sheet[s].getColumns();j++){ colData.add(sheet[s].getCell(j, i).getContents()); } row.setColData(colData); rows.add(row); } page.setRowData(rows); pageDate.add(page); } excle.setPageData(pageDate); wb.close(); is.close(); } catch (Exception e) { e.printStackTrace(); } return excle; } /** * * @param fileName 要写入文件的路径 * @param excel 要写入文件的数据 */ private static void writeExcel(String fileName,ExcelModel excel) { try { WritableWorkbook book = Workbook.createWorkbook(new File(fileName)); for(int page=0;page<excel.getPages();page++){ WritableSheet sheet = book.createSheet("第"+page+"页", 0); // 在label对象的构造方法中指名单元格位置是第一列,第一行(0,0) // 以及单元格内容为test for(int row=0;row<excel.getPageData().get(page).getRows();row++){ for(int col=0;col<excel.getPageData().get(page).getRowData().get(row).getCols();col++){ Label label = new Label(col,row ,excel.getPageData().get(page).getRowData().get(row).getColData().get(col)); // 将定义好的单元格添加到工作表中 sheet.addCell(label); } } } // 写入数据并关闭文件 book.write(); book.close(); System.out.println("生成excel文件成功"); } catch (Exception e) { e.printStackTrace(); } } }
原文地址:http://blog.csdn.net/whzhaochao/article/details/40451141