Excel IO

pom 依賴

 

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.16</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.14</version>
</dependency>
<dependency>
    <groupId>net.sourceforge.jexcelapi</groupId>
    <artifactId>jxl</artifactId>
    <version>2.6.10</version>
</dependency>

 

 

JAVA代碼

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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 org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.util.*;

public class Excel {

    private static final String EXCEL_XLS = "xls";
    private static final String EXCEL_XLSX = "xlsx";

    public static void writeExcel(List<String> keys, ArrayList<Bean> values, String finalXlsxPath) {
        OutputStream out = null;
        try {
            // 讀取Excel文檔
            File finalXlsxFile = new File(finalXlsxPath);
            Workbook workBook = getWorkbok(finalXlsxFile);
            // sheet 對應一個工作頁
            Sheet sheet = workBook.getSheetAt(0);
            //往Excel中寫新key
            Row firstRow = sheet.createRow(0);
            for (int i = 0; i < keys.size(); i++) {
                Cell first = firstRow.createCell(i);
                first.setCellValue(keys.get(i));
            }
            //往Excel中寫新values
            for (int i = 0; i < values.size(); i++) {
                Row row = sheet.createRow(i + 1);//從第二行開始,跳過屬性列
                Bean bean = values.get(i);
                Cell cell0 = row.createCell(0);//插入第一列
                cell0.setCellValue(bean.time);
                Cell cell1 = row.createCell(1);//插入第二列
                cell1.setCellValue(bean.data);
                Cell cell2 = row.createCell(2);//插入第三列
                cell2.setCellValue(bean.wal);
            }
            out = new FileOutputStream(finalXlsxPath);
            workBook.write(out);
        } catch (
                Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.flush();
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 判斷Excel的版本,獲取Workbook
     *
     * @return
     * @throws IOException
     */
    public static Workbook getWorkbok(File file) throws IOException {
        Workbook wb = null;
        FileInputStream in = new FileInputStream(file);
        if (file.getName().endsWith(EXCEL_XLS)) {     //Excel&nbsp;2003
            wb = new HSSFWorkbook(in);
        } else if (file.getName().endsWith(EXCEL_XLSX)) {    // Excel 2007/2010
            wb = new XSSFWorkbook(in);
        }
        return wb;
    }

    public static void main(String[] args) {
        Map<String, String> dataMap = new HashMap<String, String>();
        ArrayList<String> key = new ArrayList<>();//標籤
        key.add("time");
        key.add("data");
        key.add("wal");
        ArrayList<Bean> values = new ArrayList<>();//放數據
        writeExcel(key, values, "C:\\Users\\admin\\Desktop\\dataTime.xls");
    }
}

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章