用JavaBean或Servlet能生成Excel文件用于网上打印吗,如何解决?

wangxian 2002-04-29 10:36:56
用JavaBean或Servlet能生成Excel文件用于网上打印吗,如何解决?
...全文
8 点赞 收藏 8
写回复
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangwenyou 2002-05-27
http://jakarta.apache.org/poi/hssf/index.html
到这下载http://jakarta.apache.org/builds/jakarta-poi/release/
回复
wangwenyou 2002-05-27
非也,拷贝别人的代码,算什么高手
这是Apache的POI项目中使用HSSF处理excel的示例
我早让你去看了啊
“使用jakarta.apache.org的POI项目”
回复
zmrljl 2002-05-27
高手:你这段代码的文档在哪个目录copy的。
能简单介绍一下这个网站吗?
回复
zmrljl 2002-05-27
高手:你这段代码的文档在哪个目录copy的。
能简单介绍一下这个网站吗?
回复
wangwenyou 2002-05-27
唉,让你去看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 < 300; rownum++)
{
// create a row
r = s.createRow(rownum);
// on every other row
if ( (rownum % 2) == 0) {
// make the row height bigger (in twips - 1/20 of a point)
r.setHeight((short)0x249);
}

//r.setRowNum(( short ) rownum);
// create 50 cells (0-49) (the += 2 becomes apparent later
for (short cellnum = (short)0; cellnum < 50; cellnum += 2)
{
// create a numeric cell
c = r.createCell(cellnum,HSSFCell.CELL_TYPE_NUMERIC);
// do some goofy math to demonstrate decimals
c.setCellValue(rownum * 10000 + cellnum
+ ((( double ) rownum / 1000)
+ (( double ) cellnum / 10000)));

// on every other row
if ( (rownum % 2) == 0) {
// set this cell to the first cell style we defined
c.setCellStyle(cs);
}

// create a string cell (see why += 2 in the
c = r.createCell((short)(cellnum+1),HSSFCell.CELL_TYPE_STRING);

// set the cell's string value to "TEST"
c.setCellValue("TEST");
// make this column a bit wider
s.setColumnWidth((short)(cellnum+1), (short)((50*8) / ((double)1/20)) );

// on every other row
if ( (rownum % 2) == 0) {
// set this to the white on red cell style
// we defined above
c.setCellStyle(cs2);
}

}
}

//draw a thick black border on the row at the bottom using BLANKS
// advance 2 rows
rownum++;
rownum++;

r = s.createRow(rownum);

// define the third style to be the default
// except with a thick black border at the bottom
cs3.setBorderBottom(cs3.BORDER_THICK);

//create 50 cells
for (short cellnum = (short)0; cellnum < 50; cellnum++) {
//create a blank type cell (no value)
c = r.createCell(cellnum,HSSFCell.CELL_TYPE_BLANK);
// set it to the thick black border style
c.setCellStyle(cs3);
}

//end draw thick black border


// demonstrate adding/naming and deleting a sheet
// create a sheet, set its title then delete it
s = wb.createSheet();
wb.setSheetName(1,"DeletedSheet");
wb.removeSheetAt(1);
//end deleted sheet

// write the workbook to the output stream
// close our file (don't blow out our file handles
wb.write(out);
out.close();
回复
wangxian 2002-05-27
怎么没人回答啊
回复
dxxfire 2002-04-29
wangwenyou(王文友)老兄,你就不能发个源码让弟兄们学习一下啊,拜托啦。
回复
wangwenyou 2002-04-29
使用jakarta.apache.org的POI项目
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告