Java中使用poi导入/导出excel文件

1. 导入poi相关jar包(可以在apache 官网下载最新版)
poi.jar
poi-ooxml.jar
poi-ooxml-schemas.jar

2. 代码如下
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import static java.lang.System.out;


public class Util {

    // create excel with two columns, the first column are numbers 0-99, the second column are numbers 1-100
    public static void exportExcel() throws IOException {
        try {
            //create workbook
            HSSFWorkbook wb = new HSSFWorkbook();
            //create the first sheet
            HSSFSheet mappingSheet = wb.createSheet("Sheet Name");
            //header style
            HSSFCellStyle headerCellStyle = wb.createCellStyle();
            HSSFFont font = wb.createFont();
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            headerCellStyle.setFont(font);
            //body style
            HSSFCellStyle bodyCellStyle = wb.createCellStyle();
            HSSFFont bodyFont = wb.createFont();
            bodyFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
            bodyCellStyle.setFont(bodyFont);
            //header rows
            String[] mappingSheetRowTitle = { "Row Title"};
            String[] mappingSheetRowHeaderColumn = { "Column1 Title", "Column2 Title"};
            //set header
            addRow(wb, mappingSheet, headerCellStyle, 0, mappingSheetRowTitle);
            addRow(wb, mappingSheet, headerCellStyle, 2, mappingSheetRowHeaderColumn);
            //populate data
            String[] mappingData = new String[2];
            int i = 0;
            for (i = 0; i < 100; i++){
                mappingData[0] = i + "";
                mappingData[1] = (i + 1) + "";
                addRow(wb, mappingSheet, bodyCellStyle, i+3, mappingData);
            }

            OutputStream out = new FileOutputStream("C:\\test\\test.xls");
            wb.write(out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

    //add rows
    private static void addRow(HSSFWorkbook wb, HSSFSheet sheet, HSSFCellStyle cellStyle, int rowNum, String[] rowText) {
        HSSFRow row = sheet.createRow(rowNum);
        for (int i = 0; i < rowText.length; i++) {
            addCell(row, cellStyle, i, rowText[i]);
        }
    }

    //add columns
    private static void addCell(HSSFRow row, HSSFCellStyle cellStyle, int colNum, String text) {
        HSSFCell cell = row.createCell(colNum);
        cell.setCellValue(text);
        if (cellStyle != null)
            cell.setCellStyle(cellStyle);
    }

    //import excel and print data in console
    public static void importExcel() throws IOException {
        FileInputStream in = null;
        try {
            in = new FileInputStream("C:\\test\\test.xls");
            Workbook wb = new HSSFWorkbook(in);
            //get the first sheet
            Sheet sheet = wb.getSheetAt(0);
            for (Row row : sheet) {
                for (Cell cell : row) {
                    out.print (cell.toString() + "    ");
                }
                out.println();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            in.close();
        }
    }
}


3.常见的错误
如:已运行xlRead() : org.apache.poi.poifs.filesystem.NotOLE2FileException: Invalid header signature; read 0x0000000000000000, expected 0xE11AB1A1E011CFD0 - Your file appears not to be a valid OLE2 document 这个一般有两种原因,有excel表格的首字段没设置,导致替换时出现错误, 还有中原因是excel版本问题,这个只支持.xls格式的

4.更多博文
a. http://gaochun091024.blog.51cto.com/6643038/1242195
b. http://soukenan.blog.51cto.com/5130995/1188971
c. http://blog.csdn.net/u013766436/article/details/50908073
d. http://meigesir.iteye.com/blog/1539358(文件下载)

猜你喜欢

转载自pengyao0305.iteye.com/blog/2378782