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();
        }
    }
  • 相关阅读:
    pxeconfig 4.2.0 发布,PXE 首要启动设备
    Nutz 1.b.48 发布,Java 应用框架
    Exponent CMS 2.2.0 Beta3 发布
    持续交付加速了创新的步伐
    Fork CMS 3.5.1 发布,PHP 的 CMS 系统
    Org Mode 8.0 发布,TODO 管理工具
    Apache OpenNLP 1.5.3 发布
    结巴分词 0.27 发布,Python 中文分词组件
    Linux 内核 3.9 开发统计
    红帽发布开源的 OpenStack RDO
  • 原文地址:https://www.cnblogs.com/gdou/p/11801748.html
Copyright © 2011-2022 走看看