当前位置:软件学习 > Word >>

poi读写excel和word

如果在项目中想把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数据:
view plaincopy to clipboardprint?
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());  
    }  

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文件:
view plaincopy to clipboardprint?
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);  
    

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,