java生成excel

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

packagetest.poi;importjava.io.File;importjava.io.FileOutputStream;importjava.io.OutputStream;importjava.util.ArrayList;importjava.util.List;importjava.util.Map;importjxl.Workbook;importjxl.format.Alignment;importjxl.format.Border;importjxl.format.BorderLineStyle;importjxl.format.Colour;importjxl.format.UnderlineStyle;importjxl.write.Label;importjxl.write.WritableCellFormat;importjxl.write.WritableFont;importjxl.write.WritableSheet;importjxl.write.WritableWorkbook;publicclasscreateExcel{publicstaticvoidmain(String[] args) {

createExcel();

}publicstaticvoidcreateExcel() {try{

File file=newFile("f://text.xls");

OutputStream os=newFileOutputStream(file);

WritableWorkbook wwb=Workbook.createWorkbook(os);

WritableSheet ws= wwb.createSheet("sheet1", 0);//根据月份获取对象集List<Map<String,String>> list=newArrayList<Map<String,String>>();//设置单元格格式WritableFont wf =newWritableFont(WritableFont.ARIAL, 11,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.WHITE);

WritableCellFormat cellFormat=newWritableCellFormat(wf);

cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN,Colour.BLUE_GREY);

cellFormat.setAlignment(Alignment.CENTRE);

cellFormat.setBackground(Colour.BLUE);//第一行Label label =newLabel(0, 0, "序号");

label.setCellFormat(cellFormat);

ws.addCell(label);

label=newLabel(1, 0, "姓名 ");

label.setCellFormat(cellFormat);

ws.addCell(label);//合并单元格ws.mergeCells(2, 0, 3, 0);

label=newLabel(2, 0, "合并单元格");

label.setCellFormat(cellFormat);

ws.addCell(label);//第二行label =newLabel(0, 1, "1");

ws.addCell(label);

label=newLabel(1, 1, "姓名");

ws.addCell(label);

label=newLabel(2, 1, "合并1");

ws.addCell(label);

label=newLabel(3, 1, "合并2");

ws.addCell(label);//第三行label =newLabel(0, 2, "2");

ws.addCell(label);

label=newLabel(1, 2, "姓名");

ws.addCell(label);

label=newLabel(2, 2, "合并1");

ws.addCell(label);

label=newLabel(3, 2, "合并2");

ws.addCell(label);//添加数据wwb.write();

wwb.close();

System.out.PRintln("生成信息表(Excel格式)成功");

}catch(Exception e) {

System.out.println("生成信息表(Excel格式)时出错:");

e.printStackTrace();

}

}

}

操作excel需用到jxl.jar这个jar包。

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航