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 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(new
File(realpath),workbookSettings); // 建立工作簿
fos = new FileOutputStream(outFile); // 输出Excel文件
wwb = Workbook.createWorkbook(fos,wb);
// 第一个参数是输出文件,第二个参数是模板文件
WritableSheet sheet = wwb.getSheet(0); // 获得模板文件的Sheet
WritableFont 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= new
WritableFont(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= new
WritableFont(WritableFont.createFont("Arial"),10,WritableFont.BOLD);
WritableCellFormat cellFormat3 = new
WritableCellFormat(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= new
WritableFont(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= new
WritableFont(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= new
WritableFont(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= new
WritableFont(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){
if(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);
}
}
for(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 = new
Label(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 = new
Label(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 = new
Label(1,14+j,itemsDto.getXh(),cellFormat5);
sheet.addCell(B6);
Label B7 = new
Label(2,14+j,itemsDto.getWwms(),cellFormat5);
sheet.addCell(B7);
Label B8 = new
Label(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;
}
}
http://www.beijianbest.com/ 阀门 不锈钢阀门 碳钢阀门 提供
本文档为【java输出Excel文件】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。