当前位置: 技术问答>java相关
用JavaBean或Servlet能生成Excel文件用于网上打印吗,如何解决?
来源: 互联网 发布时间:2015-06-09
本文导语: 用JavaBean或Servlet能生成Excel文件用于网上打印吗,如何解决? | 唉,让你去看POI了啊,以下摘自POI文档 // create a new file FileOutputStream out = new FileOutputStream("/home...
用JavaBean或Servlet能生成Excel文件用于网上打印吗,如何解决?
|
唉,让你去看POI了啊,以下摘自POI文档
// create a new file
FileOutputStream out = new FileOutputStream("/home/me/myfile.xls");
// create a new workbook
HSSFWorkbook wb = new HSSFWorkbook();
// create a new sheet
HSSFSheet s = wb.createSheet();
// declare a row object reference
HSSFRow r = null;
// declare a cell object reference
HSSFCell c = null;
// create 3 cell styles
HSSFCellStyle cs = wb.createCellStyle();
HSSFCellStyle cs2 = wb.createCellStyle();
HSSFCellStyle cs3 = wb.createCellStyle();
// create 2 fonts objects
HSSFFont f = wb.createFont();
HSSFFont f2 = wb.createFont();
//set font 1 to 12 point type
f.setFontHeightInPoints((short)12);
//make it red
f.setColor((short)0xA);
// make it bold
//arial is the default font
f.setBoldweight(f.BOLDWEIGHT_BOLD);
//set font 2 to 10 point type
f2.setFontHeightInPoints((short)10);
//make it the color at palette index 0xf (white)
f2.setColor((short)0xf);
//make it bold
f2.setBoldweight(f2.BOLDWEIGHT_BOLD);
//set cell stlye
cs.setFont(f);
//set the cell format see HSSFDataFromat for a full list
cs.setDataFormat(HSSFDataFormat.getFormat("($#,##0_);[Red]($#,##0)"));
//set a thin border
cs2.setBorderBottom(cs2.BORDER_THIN);
//fill w fg fill color
cs2.setFillPattern((short)1);
// set foreground fill to red
cs2.setFillForegroundColor((short)0xA);
// set the font
cs2.setFont(f2);
// set the sheet name to HSSF Test
wb.setSheetName(0,"HSSF Test");
// create a sheet with 300 rows (0-299)
for (rownum = (short)0; rownum
// create a new file
FileOutputStream out = new FileOutputStream("/home/me/myfile.xls");
// create a new workbook
HSSFWorkbook wb = new HSSFWorkbook();
// create a new sheet
HSSFSheet s = wb.createSheet();
// declare a row object reference
HSSFRow r = null;
// declare a cell object reference
HSSFCell c = null;
// create 3 cell styles
HSSFCellStyle cs = wb.createCellStyle();
HSSFCellStyle cs2 = wb.createCellStyle();
HSSFCellStyle cs3 = wb.createCellStyle();
// create 2 fonts objects
HSSFFont f = wb.createFont();
HSSFFont f2 = wb.createFont();
//set font 1 to 12 point type
f.setFontHeightInPoints((short)12);
//make it red
f.setColor((short)0xA);
// make it bold
//arial is the default font
f.setBoldweight(f.BOLDWEIGHT_BOLD);
//set font 2 to 10 point type
f2.setFontHeightInPoints((short)10);
//make it the color at palette index 0xf (white)
f2.setColor((short)0xf);
//make it bold
f2.setBoldweight(f2.BOLDWEIGHT_BOLD);
//set cell stlye
cs.setFont(f);
//set the cell format see HSSFDataFromat for a full list
cs.setDataFormat(HSSFDataFormat.getFormat("($#,##0_);[Red]($#,##0)"));
//set a thin border
cs2.setBorderBottom(cs2.BORDER_THIN);
//fill w fg fill color
cs2.setFillPattern((short)1);
// set foreground fill to red
cs2.setFillForegroundColor((short)0xA);
// set the font
cs2.setFont(f2);
// set the sheet name to HSSF Test
wb.setSheetName(0,"HSSF Test");
// create a sheet with 300 rows (0-299)
for (rownum = (short)0; rownum