POI 操做 excel表格 (簡單整理)

 

簡單的整理:html

import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.streaming.SXSSFCell; import org.apache.poi.xssf.streaming.SXSSFRow; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; /** * Created by Administrator on 2019/6/12. */
public class SXSSFWorkbook_Main { public static void main(String[] args) { long startTime = System.currentTimeMillis(); String filePath = "E:\\111.xlsx"; SXSSFWorkbook sxssfWorkbook = null; BufferedOutputStream outputStream = null; try { //這樣表示SXSSFWorkbook只會保留100條數據在內存中,其它的數據都會寫到磁盤裏,這樣的話佔用的內存就會不多
            sxssfWorkbook = new SXSSFWorkbook(getXSSFWorkbook(filePath),100); //獲取第一個Sheet頁
            SXSSFSheet sheet = sxssfWorkbook.getSheetAt(0); //合併行以後建立第一行
            sheet.addMergedRegion(new CellRangeAddress(0,0,0,3)); SXSSFRow row = sheet.createRow(0); //添加第一行的單元格
            SXSSFCell cell = row.createCell(0); CellStyle cellStyle = getHeadStyle(sxssfWorkbook); cell.setCellStyle(cellStyle); row.createCell(1).setCellStyle(cellStyle); row.createCell(2).setCellStyle(cellStyle); row.createCell(3).setCellStyle(cellStyle);   //合併單元格以後要加邊框, 因此都要加上
            cell.setCellValue("項目工做清單"); //合併單元格以後設置值 //建立第2行
            SXSSFRow row1 = sheet.createRow(1); CellStyle style = getHeadStyle(sxssfWorkbook); style.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex()); row1.createCell(0).setCellStyle(style); outputStream = new BufferedOutputStream(new FileOutputStream(filePath)); sxssfWorkbook.write(outputStream); outputStream.flush(); sxssfWorkbook.dispose();// 釋放workbook所佔用的全部windows資源
        } catch (IOException e) { e.printStackTrace(); }finally { if(outputStream!=null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } long endTime = System.currentTimeMillis(); System.out.println(endTime-startTime); } private static CellStyle getHeadStyle(SXSSFWorkbook sxssfWorkbook) { CellStyle cellStyle = sxssfWorkbook.createCellStyle(); cellStyle.setAlignment(HorizontalAlignment.CENTER); //設置單元格的水平居中 //上下左右的邊框
 cellStyle.setBorderTop(BorderStyle.THIN); cellStyle.setBorderBottom(BorderStyle.THIN); cellStyle.setBorderLeft(BorderStyle.THIN); cellStyle.setBorderRight(BorderStyle.THIN); //設置單元格背景填充顏色
 cellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); return cellStyle; } public static XSSFWorkbook getXSSFWorkbook(String filePath) { XSSFWorkbook workbook =  null; BufferedOutputStream outputStream = null; try { File fileXlsxPath = new File(filePath); outputStream = new BufferedOutputStream(new FileOutputStream(fileXlsxPath)); workbook = new XSSFWorkbook(); workbook.createSheet("測試Sheet"); workbook.write(outputStream); } catch (Exception e) { e.printStackTrace(); }finally { if(outputStream!=null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return workbook; } }

 

官網嚮導:https://poi.apache.org/components/spreadsheet/quick-guide.html#FillsAndFrillsjava

官網API:http://poi.apache.org/apidocs/dev/org/apache/poi/xssf/streaming/SXSSFWorkbook.htmlapache

相關文章
相關標籤/搜索