jxl操作Excle

比较两个Excle文件中内容是否一致,即读操作

public class ExcelServlet extends HttpServlet {	

	
	private static final long serialVersionUID = 2688124847613698710L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    	
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control","no-cache");
		String oneFile=request.getParameter("oneFile");
		String towFile=request.getParameter("towFile");
		int columnCount=Integer.parseInt(request.getParameter("column"));//列数
		int row=Integer.parseInt(request.getParameter("row"))-1;//从第几行开始读
		PrintWriter out = response.getWriter();
		out.print("<response>");
			Workbook rwb2=null;
			Workbook rwb1=null;
			boolean sing =true;
			InputStream is1 = new FileInputStream(oneFile);
			InputStream is2 = new FileInputStream(towFile);
			try{
		        rwb1 = Workbook.getWorkbook(is1);
		        rwb2 = Workbook.getWorkbook(is2);
			}catch(BiffException e){
				out.print("<message>BiffException异常</message>");
				sing=false;
			}
			 Cell cell1 = null;
	         Cell cell2 = null;
	         Sheet sheet1 = rwb1.getSheet(0);
	         Sheet sheet2 = rwb2.getSheet(0);
	       	 int rowCount=sheet1.getRows();
		       for (int i = row; i <rowCount; i++) {//行
		    	   for (int j = 0; j <columnCount; j++){//列
		    		   cell1=sheet1.getCell(j, i);
		    		   cell2=sheet2.getCell(j, i);
		    		   if(!cell1.getContents().trim().equals(cell2.getContents().trim())){
    	    			   out.print("<message>第"+(i+1)+"行 第"+(j+1)+"列,数据不一致</message>");
    	    			   sing=false;
    	    		   }
		    	   }
		       }
	    if(sing){
	    	out.print("<message>未发现不一致数据</message>");
	    }
	    rwb1.close();
	    rwb2.close();    
		out.print("</response>");
    }
}

创建Excle文件

public class PaperService {

	public static void writeExcel(OutputStream os,List<SubjectVo> list) {
		try {
			/**
			 * 只能通过API提供的工厂方法来创建Workbook,而不能使用WritableWorkbook的构造函数,
			 * 因为类WritableWorkbook的构造函数为protected类型
			 * method(1)直接从目标文件中读取WritableWorkbook wwb =
			 * Workbook.createWorkbook(new File(targetfile)); method(2)如下实例所示
			 * 将WritableWorkbook直接写入到输出流
			 * 
			 */
			WritableWorkbook wwb = Workbook.createWorkbook(os);
			// 创建Excel工作表 指定名称和位置
			WritableSheet ws = wwb.createSheet("试题模版", 0);

			// **************往工作表中添加数据*****************
			
			WritableFont wf =new WritableFont(WritableFont.TIMES, 12,WritableFont.BOLD, false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED);
			WritableCellFormat wcf=new WritableCellFormat(wf);
			Label labelcf =null;
			String title[]={"所属内容","题目","试题类型","A","B","C","D","正确答案","分值"};
			//创建第一行----标题
			for(int i=0;i<title.length;i++){
				labelcf = new Label(i, 0, title[i], wcf);
				ws.addCell(labelcf);
			}
			//创建试题内容
			wf =new WritableFont(WritableFont.TIMES, 12);
			wcf=new WritableCellFormat(wf);
			for(int row=0;row<list.size();row++){
				labelcf = new Label(0, row+1, list.get(row).getTitle(), wcf);
				ws.addCell(labelcf);
				labelcf = new Label(1, row+1, list.get(row).getTitles(), wcf);
				ws.addCell(labelcf);
				labelcf = new Label(2, row+1, list.get(row).getType(), wcf);
				ws.addCell(labelcf);
				labelcf = new Label(3, row+1, list.get(row).getA(), wcf);
				ws.addCell(labelcf);
				labelcf = new Label(4, row+1, list.get(row).getB(), wcf);
				ws.addCell(labelcf);
				labelcf = new Label(5, row+1, list.get(row).getC(), wcf);
				ws.addCell(labelcf);
				labelcf = new Label(6, row+1, list.get(row).getD(), wcf);
				ws.addCell(labelcf);
				labelcf = new Label(7, row+1, list.get(row).getDaan(), wcf);
				ws.addCell(labelcf);
				labelcf = new Label(8, row+1, list.get(row).getFenzhi(), wcf);
				ws.addCell(labelcf);
			}
			wwb.write();
			wwb.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

注意:在对Excle进行操作时,行和列的下标均从0开始

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值