Java的poi技术读取和导入Excel

Eave 2016.08.10 23:13

需要使用到的JAR包:

poi-3.17.jar

poi-ooxml-3.17.jar

poi-ooxml-schemas-3.17.jar

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Excel
{
    public static void main(String[] args) throws IOException
    {
        List orders = readExcel("qianyan.xlsx");
        for(Order order : orders)
        {
            System.out.println(order.toString());
        }
    }

    @SuppressWarnings("resource")
    public static List readExcel(String file) throws IOException
    {
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(file);

        Order order = null;
        List list = new ArrayList();
        for(int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet ++)
        {
            XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);
            if(xssfSheet == null)
            {
                continue;
            }

            // Read the Row
            for(int rowNum = 0; rowNum <= xssfSheet.getLastRowNum(); rowNum ++)
            {
                XSSFRow xssfRow = xssfSheet.getRow(rowNum);
                if(xssfRow != null)
                {
                    order = new Order();
                    XSSFCell orderId = xssfRow.getCell(0);
                    XSSFCell merchantName = xssfRow.getCell(1);
                    XSSFCell nick = xssfRow.getCell(2);
                    XSSFCell mobile = xssfRow.getCell(3);
                    XSSFCell packageName = xssfRow.getCell(4);
                    XSSFCell month = xssfRow.getCell(5);
                    XSSFCell money = xssfRow.getCell(6);
                    XSSFCell payMethod = xssfRow.getCell(7);
                    XSSFCell status = xssfRow.getCell(8);

                    order.setOrderId(getValue(orderId));
                    order.setMerchantName(getValue(merchantName));
                    order.setNick(getValue(nick));
                    order.setMobile(getValue(mobile));
                    order.setPackageName(getValue(packageName));
                    order.setMonth(getValue(month));
                    order.setMoney(getValue(money));
                    order.setPayMethod(getValue(payMethod));
                    order.setStatus(getValue(status));
                    list.add(order);
                }
            }
        }

        return list;
    }

    @SuppressWarnings("resource")
    public static void whriteExcel() throws FileNotFoundException, IOException
    {
        // 表格标题名
        String title = "Sheet1";
        String filePath = "qianyan.xlsx";
        // 创建一个webbook,对应一个Excel文件
        XSSFWorkbook workbook = new XSSFWorkbook();
        // 在webbook中添加一个sheet,对应Excel文件中的sheet
        XSSFSheet sheet = workbook.createSheet(title);

        // 设置字体
        XSSFFont font = workbook.createFont();
        font.setFontHeightInPoints((short) 10);
        font.setFontName("微软雅黑");

        // 设置样式
        XSSFCellStyle style = workbook.createCellStyle();
        style.setFont(font);

        // 设置默认宽
        sheet.setDefaultColumnWidth(15);

        for(int i = 0; i < 10; i++)
        {
            XSSFRow row = sheet.createRow(i);
            for(int j = 0; j < 11; j++)
            {
                XSSFCell cell = row.createCell(j);
                cell.setCellStyle(style);
                if(i == 0)
                {
                    // 首行
                    cell.setCellValue("column" + j);
                }
                else
                {
                    // 数据
                    cell.setCellValue(String.valueOf("aiqianyan" + (i + j)));
                }
            }
        }
        FileOutputStream out = new FileOutputStream(filePath);
        workbook.write(out);
        out.close();
    }

    private static String getValue(XSSFCell xssfCell)
    {
        return String.valueOf(xssfCell.getStringCellValue()).trim();
    }
}