java输出Excel文件

  • 格式:pdf
  • 大小:262.22 KB
  • 文档页数:5

Java使用jxl输出Excel文件Java使用jxl输出Excel文件,只对office2003起作用,我尝试对office2007操作,但很多命令不起作用.所以要想达到预想的效果,还是使用office2003。

首先要下载jxl的jar文件,导入到Eclipse中。

下面是我的一个例子,第一个参数outFile:Excel文件输出到什么地方,包括Excel的文件名,如D:/re.xls。

第二个和第三个参数都是输出的内容,如果你数据比较简单,可以只要第二个参数就行。

public boolean writeExcelByJXL(String outFile,ImtesDto dto, List<MxDto> list) throws IOException {String realpath = "D:/wl/mb.xls"; //模板文件WritableWorkbook wwb;FileOutputStream fos;try {WorkbookSettings workbookSettings = new WorkbookSettings();workbookSettings.setEncoding("ISO-8859-1");Workbook wb = Workbook.getWorkbook(newFile(realpath),workbookSettings); // 建立工作簿fos = new FileOutputStream(outFile); // 输出Excel文件wwb = Workbook.createWorkbook(fos,wb);// 第一个参数是输出文件,第二个参数是模板文件WritableSheet sheet = wwb.getSheet(0); //获得模板文件的SheetWritableFont font1= new WritableFont(WritableFont.createFont("宋体"),10,WritableFont.NO_BOLD);WritableCellFormat cellFormat1 = new WritableCellFormat(font1);cellFormat1.setWrap(true);Label A1 = new Label(0,3,"公司名称:"+dto.getKhm(),cellFormat1);sheet.addCell(A1);Label A2 = new Label(0,4,"收货地址:"+dto.getShdz(),cellFormat1);sheet.addCell(A2);Label A3 = new Label(0,5,"联系人:"+dto.getLxr(),cellFormat1);sheet.addCell(A3);Label A4 = new Label(0,5,"电话:"+dto.getDh(),cellFormat1);sheet.addCell(A4);Label A5 = new Label(0,6,"传真:"+dto.getCz(),cellFormat1);sheet.addCell(A5);Date date = new Date();SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");String d = sdf.format(date.getTime());Label A6 = new Label(2,8,"日期:"+d,cellFormat1);sheet.addCell(A6);WritableFont font2= newWritableFont(WritableFont.createFont("宋体"),14,WritableFont.BOLD);WritableCellFormat cellFormat2 = new WritableCellFormat(font2);cellFormat2.setAlignment(jxl.format.Alignment.LEFT);cellFormat2.setWrap(true);Label A7 = new Label(0,8,"订单号:"+dto.getDdh(),cellFormat2);sheet.addCell(A7);Label A8 = new Label(0,9,"业务员:"+dto.getXm(),cellFormat2);sheet.addCell(A8);Label A9 = new Label(0,10,"客户号:"+dto.getKhh(),cellFormat1);sheet.addCell(A9);WritableFont font= newWritableFont(WritableFont.createFont("Arial"),10,WritableFont.BOLD);WritableCellFormat cellFormat3 = newWritableCellFormat(font);cellFormat3.setBorder(Border.TOP,jxl.format.BorderLineStyle.MEDIUM); //顶部边框为中等粗线cellFormat3.setBorder(Border.RIGHT,jxl.format.BorderLineStyle.THIN); //Border右边为细线cellFormat3.setBorder(Border.BOTTOM,jxl.format.BorderLineStyle.THIN); //cellFormat3.setAlignment(jxl.format.Alignment.CENTRE);cellFormat3.setVerticalAlignment(VerticalAlignment.CENTRE);cellFormat3.setWrap(true);WritableFont font31= newWritableFont(WritableFont.createFont("Arial"),10,WritableFont.NO_BOLD);WritableCellFormat cellFormat31 = new WritableCellFormat(font31);cellFormat31.setAlignment(jxl.format.Alignment.LEFT);cellFormat31.setVerticalAlignment(VerticalAlignment.CENTRE);cellFormat31.setWrap(true);WritableFont font4= newWritableFont(WritableFont.createFont("Arial"),10,WritableFont.BOLD);WritableCellFormat cellFormat4 = new WritableCellFormat(font4);cellFormat4.setBorder(Border.TOP,jxl.format.BorderLineStyle.MEDIUM); //Border鏄痡xl.format.Border\cellFormat4.setBorder(Border.RIGHT,jxl.format.BorderLineStyle.MEDIUM); //Border鏄痡xl.format.Border\cellFormat4.setBorder(Border.BOTTOM,jxl.format.BorderLineStyle.THIN); //Border鏄痡xl.format.Border\cellFormat4.setAlignment(jxl.format.Alignment.CENTRE);cellFormat4.setVerticalAlignment(VerticalAlignment.CENTRE);cellFormat4.setWrap(true);WritableFont font5= newWritableFont(WritableFont.createFont("Arial"),10,WritableFont.NO_BOLD);WritableCellFormat cellFormat5 = new WritableCellFormat(font5);cellFormat5.setBorder(Border.BOTTOM,jxl.format.BorderLineStyle.THIN); //Border鏄痡xl.format.Border\cellFormat5.setBorder(Border.RIGHT,jxl.format.BorderLineStyle.THIN); //Border鏄痡xl.format.Border\cellFormat5.setAlignment(jxl.format.Alignment.CENTRE);cellFormat5.setVerticalAlignment(VerticalAlignment.CENTRE);cellFormat5.setWrap(true);WritableFont font51= newWritableFont(WritableFont.createFont("Arial"),16,WritableFont.NO_BOLD);WritableCellFormat cellFormat51 = new WritableCellFormat(font51);cellFormat51.setBorder(Border.BOTTOM,jxl.format.BorderLineStyle.THIN); //Border鏄痡xl.format.Border\cellFormat51.setBorder(Border.RIGHT,jxl.format.BorderLineStyle.MEDIUM); //Border鏄痡xl.format.Border\cellFormat51.setAlignment(jxl.format.Alignment.CENTRE);cellFormat51.setVerticalAlignment(VerticalAlignment.CENTRE);cellFormat51.setWrap(true);if(list != null && list.size() > 0){i f(list.size() >= 1){for(int j =1; j < list.size(); j++){sheet.insertRow(15); //在第14行的位置插入一行sheet.insertRow(15);sheet.insertRow(15);sheet.insertRow(15);sheet.insertRow(15);}}f or(int i = 0; i < list.size(); i++){int j=i*5;sheet.setRowView(16+i,false);//第16+i行自动调整行高MxDto itemsDto = (MxDto)list.get(i);sheet.mergeCells(0,11+j,1,1);Label C1 = new Label(0,11+j,"发货方式:",cellFormat31);sheet.addCell(C1);Label C2 = new Label(2,11+j,"Supplier:",cellFormat31);sheet.addCell(C2);Label C3 = newLabel(3,11+j,itemsDto.getGys(),cellFormat31);sheet.addCell(C3);Label B1 = new Label(0,13+j,"Pos.",cellFormat3);sheet.addCell(B1);Label B11 = new Label(1,13+j,"Part-No.",cellFormat3);sheet.addCell(B11);Label B2 = new Label(2,13+j,"Description",cellFormat3);sheet.addCell(B2);Label B12 = newLabel(3,13+j,"Quantity"+"\n"+"(piece)",cellFormat3);sheet.addCell(B12);Label B14 = new Label(4,13+j,"Country"+"\n"+"of" +"\n"+"Origin",cellFormat4);sheet.addCell(B14);Label B5 = new Label(0,14+j,i+1+"",cellFormat5);sheet.addCell(B5);Label B6 = newLabel(1,14+j,itemsDto.getXh(),cellFormat5);sheet.addCell(B6);Label B7 = newLabel(2,14+j,itemsDto.getWwms(),cellFormat5);sheet.addCell(B7);Label B8 = newLabel(3,14+j,itemsDto.getSl(),cellFormat5);sheet.addCell(B8);Label B9 = new Label(4,14+j," ",cellFormat51);sheet.addCell(B9);}}wwb.write();wwb.close();fos.close();System.out.println("success");return true;} catch (Exception e) {e.printStackTrace();return false;}}/阀门不锈钢阀门碳钢阀门提供。