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包。