1.分析


一个excel文件可以有多页,每页excel中可以有多行,每行中可以有多列,用面向对象的思想可以把一行中的某列看作是一个String对象,一行看作是一个包含多个列的对象,一页是包含多行的对面,一个excle文件就是包含多页的对象


2.行对象的设计


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;
		}

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.



2.页对象的设计


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;
		}

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.



3.excel对象的设计


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;
		}
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.



4.excel读写工具类的设计


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();
	        }
	    }

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.



5.写测试




JXL.jar简单封装Excel读写操作_java



6.读测试



JXL.jar简单封装Excel读写操作_List_02