zoukankan      html  css  js  c++  java
  • JAVA-Excel文件操作

    使用环境:JAVA 1.8

    一、安装

    1.下载Poi包

    Apache POI

    当前最新稳定版本为3.14。下载poi-bin-3.14.zip即可。

    2.将下载下来的压缩包解压,将其中的所有jar文件,都复制到JRE路径中。

    我的路径是D:Program FilesJavajdk1.8.0_40jrelibext

    3.新建NetBeans程序,便可使用。

    二、使用

    具体使用可以参考压缩包中的doc文件夹里面的帮助文档。

    这里简单列举一下应用:

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.HashMap;
    import java.util.Map;
    import javax.swing.JOptionPane;
    import org.apache.poi.EncryptedDocumentException;
    import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
    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.ss.usermodel.WorkbookFactory;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    public class ExcelLog {
        public ExcelLog(int index) {
            createNewBook();
        }
        //新建工作表
        private void createNewBook() {
            try {
                String path = "D:\test.xlsx";
                File f = new File(path);
                File dir = f.getParentFile();
                if (!dir.exists()) {//如果文件路径不存在,则创建路径
                    dir.mkdirs();
                }
                if (!f.exists()) {//如果文件不存在,则新建文件
                    Workbook wb = new XSSFWorkbook(); //.xlsx
                    Sheet sheet = wb.createSheet("Test Log");  //新建Sheet
                    Row row = sheet.createRow(0);  //写一行数据
                    String[] header = {"COL1", "COL2", "COL3", "COL4", "COL5"};
                    for (int i = 0; i < header.length; i++) {
                        row.createCell(i).setCellValue(header[i]);
                    }
                    
                    //写入文件
                    try (FileOutputStream fileOut = new FileOutputStream(path)) {
                        wb.write(fileOut);
                        wb.close();
                    }
                }
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "无法写入Excel文件:" + ex.getMessage());
                System.exit(-1);
            }
        }
    
       //Sheet表的行数
        private int getRowCount() {
            try (FileInputStream fileInput = new FileInputStream(path)) {
                Workbook wb = WorkbookFactory.create(fileInput);
                Sheet sheet = wb.getSheetAt(0);
                return sheet.getPhysicalNumberOfRows();
            } catch (IOException | InvalidFormatException | EncryptedDocumentException ex) {
                JOptionPane.showMessageDialog(null, "无法读取Excel文件:" + ex.getMessage());
                System.exit(-1);
                return 0;
            }
        }
    
       //写一行数据
        private void writeLine(String[] items)  {
            try{
                Workbook wb = WorkbookFactory.create(new FileInputStream(path));
                Sheet sheet = wb.getSheetAt(0);
                int rowIndex = this.getRowCount();
                Row row = sheet.createRow(rowIndex);
                for (int i = 0; i < items.length; i++) {
                    row.createCell(i).setCellValue(items[i]);
                }
                //写入文件
                try(FileOutputStream fileOut = new FileOutputStream(path);){
                     wb.write(fileOut);
                }
                wb.close();
            } catch (IOException | InvalidFormatException | EncryptedDocumentException ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(null, "无法写入Excel文件:" + ex.getMessage());
                System.exit(-1);
            }
        }
    
    }
  • 相关阅读:
    「CF1039D」You Are Given a Tree
    「NOIP2016」换教室
    「NOIP2014」飞扬的小鸟
    「AMPPZ2014」The Prices
    POj-3104 Drying 二分+贪心
    HDOJ1312<DFS>
    STL入门2
    HDU1425 <sort 快排>
    2304: Lights Out(枚举)
    1018:放苹果(递归)
  • 原文地址:https://www.cnblogs.com/tt2015-sz/p/5468647.html
Copyright © 2011-2022 走看看