高分请教POI方面的HSSF!(0分)

L

liuzr

Unregistered / Unconfirmed
GUEST, unregistred user!
这几天正看HSSF,程序里用到,查了很多资料,包括看源网站说明,搞不太懂,反正我的机器上没有调通,基本上我整理了一下,涉及到的一些主要代码如下:
估计可能使用到的类:
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
//Excel文件的结构如下:
//一个EXCEL文件就是一个WORKBOOK,一个WORKBOOK可以包含若干个SHEET,而SHEET又是由CELL组成的。
创建workbook及两个表
HSSFWorkbook wb = new HSSFWorkbook();
//使用默认的构造方法创建workbook
HSSFSheet sheet1 = wb.createSheet("First Sheet");
HSSFSheet sheet2 = wb.createSheet("Second Sheet");
// 创建两个工作表
//生成三行,每行有五个单元,并且填入相应的值;行号,单元号从0开始
HSSFRow row0 = Sheet1.createRow((short)0);
//第一行不能有值,在EXCELL中表示字段
HSSFCell cell0 = row.createCell((short)0);
HSSFCell cell1 = row.createCell((short)1);
HSSFCell cell2 = row.createCell((short)2);
HSSFCell cell3 = row.createCell((short)3);
HSSFCell cell4 = row.createCell((short)4);

HSSFRow row1 = Sheet1.createRow((short)1);
row1.createCell((short)0).setCellValue(1.2) ;
row1.createCell((short)1).setCellValue(1) ;
row1.createCell((short)2).setCellValue(true) ;
row1.createCell((short)3).setCellValue("This is a string") ;
row1.createCell((short)4).setCellValue(2) ;
HSSFRow row2 = Sheet1.createRow((short)2);
row2.createCell((short)0).setCellValue(1.2) ;
row2.createCell((short)1).setCellValue(1) ;
row2.createCell((short)2).setCellValue(true) ;
row2.createCell((short)3).setCellValue("This is a string") ;
row2.createCell((short)4).setCellValue(2) ;

FileOutputStream fileOut = new FileOutputStream("workbook.xls");
//指定文件名
wb.write(fileOut);
//输出到文件
fileOut.close();
这是我的理解,请高手指教,或者添加说明,能使这段程序正常运行,生成可用的代码,同时也可以给以后用到的人作一个简单的例子。
解决问题者,给200,此贴不计,在另一无法回收的贴中给分,
即http://www.delphibbs.com/delphibbs/dispq.asp?lid=2245193,如果不够,另开贴给分。
 
晕倒,这样不是可以用么,
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short)0);
// Create a cell and put a date value in it. The first cell is not styled as a date.
HSSFCell cell = row.createCell((short)0);
cell.setCellValue(new Date());
// we style the second cell as a date (and time). It is important to create a new cell style from the workbook
// otherwise you can end up modifying the built in style and effecting not only this cell but other cells.
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
cell = row.createCell((short)1);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
 
楼上的兄弟,别抄例子说明,如果你在机器上有调通的程序,把生成的文档及例子程序给出来,看看,我不知道是我的机器设置的问题,不想重做机器,还是别的,按说应该没错呀,可是就是不成,如果方便的话,请你试试!
 

Similar threads

顶部