zoukankan      html  css  js  c++  java
  • POI实现文件读取与写出Excel文档

    入门demo:

    package studyPOI;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import org.apache.poi.hssf.usermodel.HSSFDataFormat;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.CellStyle;
    import org.apache.poi.ss.usermodel.CellType;
    import org.apache.poi.ss.usermodel.FillPatternType;
    import org.apache.poi.ss.usermodel.HorizontalAlignment;
    import org.apache.poi.ss.usermodel.IndexedColors;
    import org.apache.poi.ss.usermodel.Row;
    
    public class MSExcel {
        public static void main(String[] args) throws FileNotFoundException, IOException {
            new MSExcel().helloExcel();
        }
        
        private void helloExcel() throws FileNotFoundException,IOException {
            String fileStr = "F:/hello.xls";
            File file = new File(fileStr);
            FileInputStream fim = new FileInputStream(file);
            HSSFWorkbook hssfWorkbook = new HSSFWorkbook(fim);
            HSSFSheet sheet = hssfWorkbook.getSheetAt(0);
            for (Row row : sheet) {
                System.out.println("row"+row);
                for (Cell cell : row) {
                    if (cell.getCellType() == CellType.NUMERIC) {
                        double value = cell.getNumericCellValue();
                        System.out.println("number:"+value);
                    }else if (cell.getCellType() == CellType.STRING) {
                        String value = cell.getStringCellValue();
                        System.out.println("value:"+value);
                    }
                    CellStyle cellStyle = hssfWorkbook.createCellStyle();
                    System.out.println("IndexedColors.BLUE.getIndex()"+IndexedColors.BLUE.getIndex());
                    cellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
                    cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                    cellStyle.setAlignment(HorizontalAlignment.CENTER);
                    cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00%"));
                    cell.setCellValue(8.88);
                    cell.setCellStyle(cellStyle);
                }
            }
            FileOutputStream outputStream = new FileOutputStream(file);
            hssfWorkbook.write(outputStream);
            hssfWorkbook.close();
            fim.close();
        }
    }
  • 相关阅读:
    分享一道关于类、实例加载和初始化顺序的基础面试题
    IDEA部署 java Web项目 常见配置
    jsp和servlet开发过程中参数传递乱码问题总结
    Java String引起的常量池、String类型传参、“==”、“equals”、“hashCode”问题 细节分析
    利用反射创建实例强制转换为接口失败小结
    oracle与sqlserver的十大区别
    js闭包的用途 【转】
    实体框架EF笔记
    利用存储过程进行分页
    关于ASP.NET运行机制原理。。。个人总结
  • 原文地址:https://www.cnblogs.com/gdou/p/11801748.html
Copyright © 2011-2022 走看看