读书人

写下excel一般步骤-包含excel中格式的

发布时间: 2012-11-06 14:07:00 作者: rapoo

写入excel一般步骤-包含excel中格式的设定【java】

File tempFile=new File("d:/temp/output.xls");WritableWorkbook workbook = Workbook.createWorkbook(tempFile);WritableSheet sheet = workbook.createSheet("TestCreateExcel", 0); //一些临时变量,用于写到excel中Label l=null;jxl.write.Number n=null;jxl.write.DateTime d=null;//预定义的一些字体和格式,同一个Excel中最好不要有太多格式WritableFont headerFont = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLUE); WritableCellFormat headerFormat = new WritableCellFormat (headerFont); WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED); WritableCellFormat titleFormat = new WritableCellFormat (titleFont); WritableFont detFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); WritableCellFormat detFormat = new WritableCellFormat (detFont); NumberFormat nf=new NumberFormat("0.00000");//用于Number的格式WritableCellFormat priceFormat = new WritableCellFormat (detFont, nf); DateFormat df=new DateFormat("yyyy-MM-dd");//用于日期的WritableCellFormat dateFormat = new WritableCellFormat (detFont, df); //剩下的事情,就是用上面的内容和格式创建一些单元格,再加到sheet中l=new Label(0, 0, "用于测试的Excel文件", headerFormat);sheet.addCell(l);//add Titleint column=0;l=new Label(column++, 2, "标题", titleFormat);sheet.addCell(l);l=new Label(column++, 2, "日期", titleFormat);sheet.addCell(l);l=new Label(column++, 2, "货币", titleFormat);sheet.addCell(l);l=new Label(column++, 2, "价格", titleFormat);sheet.addCell(l);//add detailint i=0;column=0;l=new Label(column++, i+3, "标题 "+i, detFormat);sheet.addCell(l);d=new DateTime(column++, i+3, new java.util.Date(), dateFormat);sheet.addCell(d);l=new Label(column++, i+3, "CNY", detFormat);sheet.addCell(l);n=new jxl.write.Number(column++, i+3, 5.678, priceFormat);sheet.addCell(n);i++;column=0;l=new Label(column++, i+3, "标题 "+i, detFormat);sheet.addCell(l);d=new DateTime(column++, i+3, new java.util.Date(), dateFormat);sheet.addCell(d);l=new Label(column++, i+3, "SGD", detFormat);sheet.addCell(l);n=new jxl.write.Number(column++, i+3, 98832, priceFormat);sheet.addCell(n);//设置列的宽度column=0;sheet.setColumnView(column++, 20);sheet.setColumnView(column++, 20);sheet.setColumnView(column++, 10);sheet.setColumnView(column++, 20);workbook.write();workbook.close();

读书人网 >编程

热点推荐