POI of Apache

现在所做的一个项目中,有一个功能为从Excel表里把数据导入到SQLServer2000,开发语言为java,在网上找了很久,终于看到了Apache的一个开源项目poi里有这么一个功能的实现。如果有谁会用c#实现这功能,希望能分享一下。

为什么选择 POI?
一个十分令人沮丧的事实就是难以与 Microsoft 的专有文件格式进行交互操作。但 Apache Software Foundation 通过称作 POI(POI 代表 Poor Obfuscation Implementation, 即不良模糊化实现)的项目解决了该问题。POI 的目标就是提供一组 Java API 来使得基于 Microsoft OLE 2 Compound Document 格式的 Microsoft Office 文件易于操作。一些 POI API 仅仅是为最常用的 Microsoft Office 文件 Word 和 Excel 而开发的;而其他的 API 则是用于通用的 OLE 2 Compound Document 和属性文件。Apache POI 网站提供了丰富的关于 POI 项目及其 API 的附加信息,其网址为 http://jakarta.apache.org/poi/index.html

Reading and Rewriting Workbooks

    POIFSFileSystem fs      =
new POIFSFileSystem(new FileInputStream("workbook.xls"));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(2);
HSSFCell cell = row.getCell((short)3);
if (cell == null)
cell = row.createCell((short)3);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("a test");

// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();


Insert data to database


for(int row = 0; row < spreadsheetModel.getRowCount(); row++) {
sqlRowInsertQuery = "INSERT INTO " + sqlFacade.getUser()
+ "." + spreadsheetName + sqlColumnNames + " VALUES (";
for(int col = 0; col < spreadsheetModel.getColumnCount()-1; col++) {
if (col > 0) {
sqlRowInsertQuery += ", ";
}
sqlRowInsertQuery += "'" +
((HSSFCell)(spreadsheetModel.getRow(row).get(col))).getStringCellValue() + "'";
}
sqlRowInsertQuery += " ); ";


已经实现了从Excel表向数据库导入数据。
原码下载
posted @ 2005-11-01 11:40  电视机9号  阅读(615)  评论(0编辑  收藏  举报