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

Java POI操作Excel

apache.org提供了一个项目(poi)专门用于对办公软件的操作。这样大大方便了我们操作和读取其中的内容,用于统计分析。版本为poi-bin-3.7-20101029.zip
Component APIs
Excel (SS=HSSF+XSSF)
Word (HWPF+XWPF)
PowerPoint (HSLF+XSLF)
OpenXML4J (OOXML)
OLE2 Filesystem (POIFS)
OLE2 Document Props (HPSF)
Outlook (HSMF)
Visio (HDGF)
Publisher (HPBF)

 

在文档中提供了2种不同的写入方法,一个是老代码,一个是新代码。如果你希望能够兼容xls文件和xslx文件,那么建议用新代码。且老代码对office2007的操作不是很好。

代码如下:

  public static void main(String[] args)throws Exception {
  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 2 cell styles 创建2个单元格样式
  HSSFCellStyle cs = wb.createCellStyle();
  HSSFCellStyle cs2 = wb.createCellStyle();
  HSSFDataFormat df = wb.createDataFormat();

  // create 2 fonts objects 创建2个单元格字体
  HSSFFont f = wb.createFont();
  HSSFFont f2 = wb.createFont();

  // Set font 1 to 12 point type, blue and bold 设置字体类型1到12号,蓝色和粗体
  f.setFontHeightInPoints((short) 12);
  f.setColor( HSSFColor.RED.index );
  f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

  // Set font 2 to 10 point type, red and bold 设置字体类型2到10号,红色和粗体
  f2.setFontHeightInPoints((short) 10);
  f2.setColor( HSSFColor.RED.index);
  f2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

  // Set cell style and formatting 设置单元格样式和格式
  cs.setFont(f);
  cs.setDataFormat(df.getFormat("#,##0.0"));

  // Set the other cell style and formatting 设置其他单元格样式和格式
  cs2.setBorderBottom(cs2.BORDER_THIN);
  cs2.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));
  cs2.setFont(f2);


  // Define a few rows 定义一个新行
  for(short rownum = (short)0; rownum < 30; rownum++) {
    r = s.createRow(rownum);
   for(short cellnum = (short)0; cellnum < 10; cellnum += 2) {
     c = r.createCell(cellnum);
    HSSFCell c2 = r.createCell(cellnum+1);

    c.setCellValue((double)rownum + (cellnum/10));
    c2.setCellValue(new HSSFRichTextString("Hello! " + cellnum));
   }
  }

  // Save保存
  FileOutputStream out = new FileOutputStream("d://workbook.xls");
  wb.write(out);
  out.close();
         
}

---------------------------------------------------------------------------------------------------------
  
public static void main(String[] args) throws Exception {
  Workbook[] wbs = new Workbook[] { new HSSFWorkbook(),
    new XSSFWorkbook() };
  for (int i = 0; i < wbs.length; i++) {
   Workbook wb = wbs[i];
   CreationHelper createHelper = wb.getCreationHelper();

   // create a new sheet 创建一个新表
   Sheet s = wb.createSheet();
   // declare a row object reference 声明一个对象的引用行
   Row r = null;
   // declare a cell object reference 声明对象的引用单元格
   Cell c = null;
   // create 2 cell styles 创建2单元格样式
   CellStyle cs = wb.createCellStyle();
   CellStyle cs2 = wb.createCellStyle();
   DataFormat df = wb.createDataFormat();

   // create 2 fonts objects 创建2字体对象
   Font f = wb.createFont();
   Font f2 = wb.createFont();

   // Set font 1 to 12 point type, blue and bold 设置字体1至12点类型,蓝色和粗体
   f.setFontHeightInPoints((short) 12);
   f.setColor(IndexedColors.RED.getIndex());
   f.setBoldweight(Font.BOLDWEIGHT_BOLD);

   // Set font 2 to 10 point type, red and bold 设置字体2至10点类型,红色和粗体
   f2.setFontHeightInPoints((short) 10);
   f2.setColor(IndexedColors.RED.getIndex());
   f2.setBoldweight(Font.BOLDWEIGHT_BOLD);

   // Set cell style and formatting 设置单元格样式和格式
   cs.setFont(f);
   cs.setDataFormat(df.getFormat("#,##0.0"));

   // Set the other cell style and formatting 设置其他单元格样式和格式
   cs2.setBorderBottom(cs2.BORDER_THIN);
   cs2.setDataFormat(df.getFormat("text"));
   cs2.setFont(f2);

   // Define a few rows 定义几行
   for (int rownum = 0; rownum < 30; rownum++) {
    r = s.createRow(rownum);
    for (int cellnum = 0; cellnum < 10; cellnum += 2) {
     c = r.createCell(cellnum);
     Cell c2 = r.createCell(cellnum + 1);

     c.setCellValue((double) rownum + (cellnum / 10));
     c2.setCellValue(createHelper.createRichTextString("Hello! "
       + cellnum));
    }
   }

   // Save 保存
   String filename = "d://workbook.xls";
   if (wb instanceof XSSFWorkbook) {
    filename = filename + "x";
   }

   FileOutputStream out = new FileOutputStream(filename);
   wb.write(out);
   out.close();
  }

}
---------------------------------------------------------------------------------------------------------

java读取Excel,同时兼容2003及以前版本和2007版本:

public class PoiReadExcel {

public void readExcel(String filePath) {

try {

Workbook workBook = null;
        try {
        workBook = new XSSFWorkbook(filePath); //支持2007
        } catch (Exception ex) {
        workBook = new HSSFWorkbook(new FileInputStream(filePath)); //支持2003及以前
        }

// 获得Excel中工作表个数
System.out.println("工作表个数 :" + workBook.getNumberOfSheets());

//循环每个工作表
for (int i = 0; i < workBook.getNumberOfSheets(); i++) {
// 创建工作表
Sheet sheet = workBook.getSheetAt(i);

int rows = sheet.getPhysicalNumberOfRows(); // 获得行数
if (rows > 0) {
sheet.getMargin(Sheet.TopMargin);
for (int r = 0; r < rows; r++) { // 行循环
Row row = sheet.getRow(r);
if (row != null) {

int cells = row.getLastCellNum();// 获得列数
for (short c = 0; c < cells; c++) { // 列循环
Cell cell = row.getCell(c);

if (cell != null) {
String value = getValue(cell);
System.out.println("第" + r + "行 " + "第" + c
+ "列:&qu

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