现在的位置: 首页 > 综合 > 正文

使用poi读写excel和word .

2013年10月07日 ⁄ 综合 ⁄ 共 3590字 ⁄ 字号 评论关闭

如果在项目中想把excel数据导入到数据库中或者将数据库中的数据导出为excel,POI是个不错的选择。Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft
Office格式档案读和写的功能。
HSSF - 提供读写Microsoft Excel格式档案的功能。
XSSF - 提供读写Microsoft Excel OOXML格式档案的功能。
HWPF - 提供读写Microsoft Word格式档案的功能。
HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
HDGF - 提供读写Microsoft Visio格式档案的功能。
下面是两个demo,分别读写excel和word:
读取xls数据:

package poi.xls;  
 
import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
import org.apache.poi.hssf.usermodel.HSSFSheet;  
import org.apache.poi.hssf.usermodel.HSSFRow;  
import org.apache.poi.hssf.usermodel.HSSFCell;  
import java.io.FileInputStream;  
 
/** 
* 利用POI实现从excel中读取内容 
*/ 
public class XLSReader {  
      
    public static String fileToRead = "c:/test.xls";  
 
    public static void main(String args[]) throws Exception{  
            // 创建对Excel工作簿文件的引用  
            HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToRead));  
 
            //HSSFSheet sheet = workbook.getSheet("第一页");  
            HSSFSheet sheet = workbook.getSheetAt(0);  
              
            //读取指定索引行的值  
            HSSFRow row = sheet.getRow(1);  
            //读取指定索引格的值  
            HSSFCell id = row.getCell((short) 0);  
            HSSFCell name = row.getCell((short) 1);  
            HSSFCell password = row.getCell((short) 2);  
              
            //读出数据  
            System.out.println("id: " + id.getNumericCellValue());  
            System.out.println("name:  " + name.getRichStringCellValue());  
            System.out.println("password:  " + password.getRichStringCellValue());  
    }  
} 

创建xls文件:

package poi.xls;
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
/**
* 利用POI实现向excel中写入内容
*/
public class XLSWriter {

public static String fileToWrite = "c:/test.xls";

public static void main(String[] args) throws Exception {
  // 创建新的Excel 工作簿
  HSSFWorkbook workbook = new HSSFWorkbook();
  // 在Excel工作簿中建一工作表,其名为缺省值
  HSSFSheet sheet = workbook.createSheet("第一页");
  //HSSFSheet sheet = workbook.createSheet();
  // 在指定的索引处创建一行
  HSSFRow row = sheet.createRow((short) 0);
  //在指定索引处创建单元格
  HSSFCell id = row.createCell((short) 0);
  // 定义单元格为字符串类型
  id.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
  // 在单元格中输入一些内容,HSSFRichTextString可以解决乱码问题
  HSSFRichTextString idContent = new HSSFRichTextString("用户id号");
  id.setCellValue(idContent);
  HSSFCell name = row.createCell((short) 1);
  name.setCellType(HSSFCell.CELL_TYPE_STRING);
  HSSFRichTextString nameContent = new HSSFRichTextString("用户名");
  name.setCellValue(nameContent);
  HSSFCell password = row.createCell((short) 2);
  password.setCellType(HSSFCell.CELL_TYPE_STRING);
  HSSFRichTextString passwordContent = new HSSFRichTextString("用户密码");
  password.setCellValue(passwordContent);
  // 新建一输出文件流
  FileOutputStream out = new FileOutputStream(fileToWrite);
  // 把相应的Excel 工作簿存盘
  workbook.write(out);
  out.flush();
  // 操作结束,关闭文件
  out.close();
  System.out.println("文件生成..." + fileToWrite);
}
}


读取word内容:

package poi.doc;
/**
* 利用POI实现从word中读取内容
*/
import java.io.FileInputStream;
import org.apache.poi.hwpf.extractor.WordExtractor;
public class DOCReader {

public static String fileToRead = "c:/test.doc";

public static void main(String[] args) throws Exception{
   // 创建输入流读取DOC文件
   FileInputStream in = new FileInputStream(fileToRead);
  
   // 创建WordExtractor
   WordExtractor extractor = new WordExtractor(in);
  
   // 对DOC文件进行提取
   String text = extractor.getText();
   System.out.println(text);
}
}


//创建word 文件:
package poi.doc;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
* 利用POI实现向word写入内容
*/
public class DOCWriter {
public static String fileToWrite = "c:/test.doc";
public static void main(String[] args) throws Exception{
  String content = "测试数据,将被写入文档";
 
  byte b[] = content.getBytes();
  ByteArrayInputStream bais = new ByteArrayInputStream(b);
  POIFSFileSystem fs = new POIFSFileSystem();
 
  FileOutputStream ostream = new FileOutputStream(fileToWrite);
  fs.writeFilesystem(ostream);
  bais.close();
 
  ostream.close();
}
}

抱歉!评论已关闭.