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);
            }
        }
    
    }
  • 相关阅读:
    Windows server 2016 解决“无法完成域加入,原因是试图加入的域的SID与本计算机的SID相同。”
    Windows Server 2016 辅助域控制器搭建
    Windows Server 2016 主域控制器搭建
    Net Framework 4.7.2 覆盖 Net Framework 4.5 解决办法
    SQL SERVER 2012更改默认的端口号为1772
    Windows下彻底卸载删除SQL Serever2012
    在Windows Server2016中安装SQL Server2016
    SQL Server 创建索引
    C#控制台或应用程序中两个多个Main()方法的设置
    Icon cache rebuilding with Delphi(Delphi 清除Windows 图标缓存源代码)
  • 原文地址:https://www.cnblogs.com/tt2015-sz/p/5468647.html
Copyright © 2011-2022 走看看