zoukankan      html  css  js  c++  java
  • 读取指定excel,修改并某个值并另存到指定路径

    HSSFWorkBook是解析excel2007以前的版本(xls)
    之后的版本使用XSSFWrokBook(xlsx)

    附:处理excel2007之后的版本代码:

    package gbyp.autoQuery.action;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.nio.channels.FileChannel;
    import java.util.Date;
    import java.util.Iterator;
    
    import org.apache.poi.hssf.extractor.ExcelExtractor;
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFCellStyle;
    import org.apache.poi.hssf.usermodel.HSSFDataFormat;
    import org.apache.poi.hssf.usermodel.HSSFFont;
    import org.apache.poi.hssf.usermodel.HSSFHyperlink;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.hssf.util.HSSFColor;
    import org.apache.poi.openxml4j.opc.OPCPackage;
    import org.apache.poi.poifs.filesystem.POIFSFileSystem;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.util.CellRangeAddress;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    import xsf.IContextDictionary;
    import xsf.data.DBManager;
    import xsf.data.DataRow;
    import xsf.data.DataTable;
    import xsf.data.Sql;
    import xsf.web.HttpContext;
    import xsf.web.IAction;
    
    public class ExcelExport {
    
        public static void main(String[] args) {
            ExcelExport ee = new ExcelExport();
            ee.writeExcel2();
        }
    
        public void writeExcel2() {
            // 声明excel对象
            XSSFWorkbook wb = null;
            // 声明poi流
            // POIFSFileSystem fs=null;
            // 写读取的文件路径
            String path = "E:/需要完善的项目清单.xlsx";
            try {
    
                // 创建文件流 创建excell对象
                wb = new XSSFWorkbook(new FileInputStream(path));
    
                // 获得工作薄, 得到工作表
                XSSFSheet sheet = wb.getSheetAt(0);
    
                boolean isOne = true;
                for (Iterator<Row> iter = sheet.rowIterator(); iter.hasNext();) {
                    // 得到行
                    Row row = iter.next();
                    // 迭代列
                    // 循环每一行的所有列
                    int i = 0;
                    for (Iterator<Cell> cellIter = row.cellIterator(); cellIter
                            .hasNext();) {
                        // 得到列对象
                        Cell cell = cellIter.next();
                        if (i == 1) {
                            String content = cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC ? cell
                                    .getNumericCellValue() + ""
                                    : cell.getStringCellValue();
                            if (!content.equals("项目名称")) {
                                //数据库取值并赋值
                                cell.setCellValue("****项目");
                            }
                        }
    
                        i++;
                    }
                    if (isOne) {
                        isOne = false;
                    }
                }
    
                String path2 = "E:/需要完善的项目清单2.xlsx";
                // 创建输出流
                OutputStream out = new FileOutputStream(path2);
                // 将数据写入文件
                wb.write(out);
                // 关闭文件
                out.close();
    
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    View Code

    附:处理excel2007之前版本代码:

    package gbyp.autoQuery.action;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.nio.channels.FileChannel;
    import java.util.Date;
    import java.util.Iterator;
    import org.apache.poi.hssf.extractor.ExcelExtractor;
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFCellStyle;
    import org.apache.poi.hssf.usermodel.HSSFDataFormat;
    import org.apache.poi.hssf.usermodel.HSSFFont;
    import org.apache.poi.hssf.usermodel.HSSFHyperlink;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.hssf.util.HSSFColor;
    import org.apache.poi.poifs.filesystem.POIFSFileSystem;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.util.CellRangeAddress;
    
    import xsf.IContextDictionary;
    import xsf.data.DBManager;
    import xsf.data.DataRow;
    import xsf.data.DataTable;
    import xsf.data.Sql;
    import xsf.web.HttpContext;
    import xsf.web.IAction;
    
    public class ExcelExport {
    
        public static void main(String[] args) {
            ExcelExport ee = new ExcelExport();
            ee.writeExcel2();
        }
    
        public void writeExcel2() {
            // 声明excel对象
            HSSFWorkbook wb = null;
            // 声明poi流
            POIFSFileSystem fs = null;
            // 写读取的文件路径
            String path = "E:/需要完善的项目清单.xls";
            try {
    
                // 设置要读取的文件路径
                // 创建文件流
                fs = new POIFSFileSystem(new FileInputStream(path));
    
                // HSSFWorkBook相当于一个excel文件,HSSFWorkBook是解析excel2007以前的版本(xls)
                // 之后的版本使用XSSFWrokBook(xlsx)
                // 创建excell对象
                wb = new HSSFWorkbook(fs);
    
                // 获得工作薄
                // 得到工作表
                HSSFSheet sheet = wb.getSheetAt(0);
    
                boolean isOne = true;
    
                for (Iterator<Row> iter = sheet.rowIterator(); iter.hasNext();) {
                    // 得到行
                    Row row = iter.next();
                    // 迭代列
                    // 循环每一行的所有列
                    int i = 0;
                    for (Iterator<Cell> cellIter = row.cellIterator(); cellIter.hasNext();) {
                        // 得到列对象
                        Cell cell = cellIter.next();
                        if (i == 1) {
                            String content = cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC ? cell.getNumericCellValue() + "": cell.getStringCellValue();
                            if(!content.equals("项目名称")){
                                cell.setCellValue("****项目");
                            }
                        }
    
                        i++;
                    }
                    if (isOne) {
    
                        isOne = false;
                    }
                }
    
                String path2 = "E:/需要完善的项目清单2.xls";
                // 创建输出流
                OutputStream out = new FileOutputStream(path2);
                // 将数据写入文件
                wb.write(out);
                // 关闭文件
                out.close();
    
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    }
    View Code
    你以后的气质里,藏着你走过的路,读过的书和爱过的人。 ​​​
  • 相关阅读:
    jMeter 里 CSV Data Set Config Sharing Mode 的含义详解
    如何使用 jMeter Parallel Controller
    使用 Chrome 开发者工具 coverage 功能分析 web 应用的渲染阻止资源的执行分布情况
    使用 Chrome 开发者工具的 lighthouse 功能分析 web 应用的性能问题
    关于 SAP 电商云首页加载时触发的 OCC API 请求
    SAP UI5 确保控件 id 全局唯一的实现方法
    SAP 电商云 Accelerator 和 Spartacus UI 的工作机制差异
    介绍一个好用的能让网页变成黑色背景的护眼 Chrome 扩展应用
    Chrome 开发者工具 performance 标签页的用法
    Client Side Cache 和 Server Side Cache 的区别
  • 原文地址:https://www.cnblogs.com/Sophias/p/7157519.html
Copyright © 2011-2022 走看看