首页 > 其他 > 详细

POI实现文件读取与写出Excel文档

时间:2019-11-05 22:04:22      阅读:109      评论:0      收藏:0      [点我收藏+]

入门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();
    }
}

 

POI实现文件读取与写出Excel文档

原文:https://www.cnblogs.com/gdou/p/11801748.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!