POI導出Excel和InputStream存儲為文件


POI導出Excel和InputStream存儲為文件

本文需要說明的兩個問題

  1. InputStream如何保存到某個文件夾下
  2. POI生成Excel

POI操作utils類

代碼如下。主要步驟如下:

  1. 創建workbook
  2. 創建sheet
  3. 生產表頭,並做相應的美化
  4. 將list中傳進來的數據依次添加到sheet中

POI操作類

import org.apache.poi.hssf.usermodel.*; import org.apache.poi.hssf.util.HSSFColor; import java.io.*; import java.util.ArrayList; import java.util.List; public class WriteExcel { //導出表的列名 private String[] rowName; //每行作為一個Object對象 private List<Object[]> dataList = new ArrayList<Object[]>(); //構造方法,傳入要導出的數據 public WriteExcel(String[] rowName, List<Object[]> dataList) { this.dataList = dataList; this.rowName = rowName; } /* * 導出數據 * */ public InputStream export() throws Exception { HSSFWorkbook workbook = new HSSFWorkbook(); // 創建工作簿對象 HSSFSheet sheet = workbook.createSheet("sheet1"); // 創建工作表 //sheet樣式定義【getColumnTopStyle()/getStyle()均為自定義方法 - 在下面 - 可擴展】 HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);//獲取列頭樣式對象 HSSFCellStyle style = this.getStyle(workbook); //單元格樣式對象 // 定義所需列數 int columnNum = rowName.length; HSSFRow rowRowName = sheet.createRow(0); // 在索引2的位置創建行(最頂端的行開始的第二行) // 將列頭設置到sheet的單元格中 for (int n = 0; n < columnNum; n++) { HSSFCell cellRowName = rowRowName.createCell(n); //創建列頭對應個數的單元格 cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); //設置列頭單元格的數據類型 HSSFRichTextString text = new HSSFRichTextString(rowName[n]); cellRowName.setCellValue(text); //設置列頭單元格的值 cellRowName.setCellStyle(columnTopStyle); //設置列頭單元格樣式 } //將查詢出的數據設置到sheet對應的單元格中 for (int i = 0; i < dataList.size(); i++) { Object[] obj = dataList.get(i);//遍歷每個對象 HSSFRow row = sheet.createRow(i + 1);//創建所需的行數 for (int j = 0; j < obj.length; j++) { HSSFCell cell = null; //設置單元格的數據類型 cell = row.createCell(j, HSSFCell.CELL_TYPE_STRING); if (!"".equals(obj[j]) && obj[j] != null) { cell.setCellValue(obj[j].toString()); //設置單元格的值 } cell.setCellStyle(style); //設置單元格樣式 } } //讓列寬隨着導出的列長自動適應 for (int colNum = 0; colNum < columnNum; colNum++) { int columnWidth = sheet.getColumnWidth(colNum) / 256; for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { HSSFRow currentRow; //當前行未被使用過 if (sheet.getRow(rowNum) == null) { currentRow = sheet.createRow(rowNum); } else { currentRow = sheet.getRow(rowNum); } if (currentRow.getCell(colNum) != null) { HSSFCell currentCell = currentRow.getCell(colNum); if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) { int length = currentCell.getStringCellValue().getBytes().length; if (columnWidth < length) { columnWidth = length; } } } } if (colNum == 0) { sheet.setColumnWidth(colNum, (columnWidth - 2) * 256); } else { sheet.setColumnWidth(colNum, (columnWidth + 4) * 256); } } ByteArrayOutputStream os = new ByteArrayOutputStream(); try { workbook.write(os); } catch (IOException e) { e.printStackTrace(); } byte[] content = os.toByteArray(); InputStream is = new ByteArrayInputStream(content); return is; } /* * 列頭單元格樣式 */ public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) { // 設置字體 HSSFFont font = workbook.createFont(); //設置字體大小 font.setFontHeightInPoints((short) 11); //字體加粗 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //設置字體名字 font.setFontName("Courier New"); //設置樣式; HSSFCellStyle style = workbook.createCellStyle(); //設置底邊框; style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //設置底邊框顏色; style.setBottomBorderColor(HSSFColor.BLACK.index); //設置左邊框; style.setBorderLeft(HSSFCellStyle.BORDER_THIN); //設置左邊框顏色; style.setLeftBorderColor(HSSFColor.BLACK.index); //設置右邊框; style.setBorderRight(HSSFCellStyle.BORDER_THIN); //設置右邊框顏色; style.setRightBorderColor(HSSFColor.BLACK.index); //設置頂邊框; style.setBorderTop(HSSFCellStyle.BORDER_THIN); //設置頂邊框顏色; style.setTopBorderColor(HSSFColor.BLACK.index); //在樣式用應用設置的字體; style.setFont(font); //設置自動換行; style.setWrapText(false); //設置水平對齊的樣式為居中對齊; style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //設置垂直對齊的樣式為居中對齊; style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); return style; } /* * 列數據信息單元格樣式 */ public HSSFCellStyle getStyle(HSSFWorkbook workbook) { // 設置字體 HSSFFont font = workbook.createFont(); //設置字體大小 //font.setFontHeightInPoints((short)10); //字體加粗 //font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //設置字體名字 font.setFontName("Courier New"); //設置樣式; HSSFCellStyle style = workbook.createCellStyle(); //設置底邊框; style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //設置底邊框顏色; style.setBottomBorderColor(HSSFColor.BLACK.index); //設置左邊框; style.setBorderLeft(HSSFCellStyle.BORDER_THIN); //設置左邊框顏色; style.setLeftBorderColor(HSSFColor.BLACK.index); //設置右邊框; style.setBorderRight(HSSFCellStyle.BORDER_THIN); //設置右邊框顏色; style.setRightBorderColor(HSSFColor.BLACK.index); //設置頂邊框; style.setBorderTop(HSSFCellStyle.BORDER_THIN); //設置頂邊框顏色; style.setTopBorderColor(HSSFColor.BLACK.index); //在樣式用應用設置的字體; style.setFont(font); //設置自動換行; style.setWrapText(false); //設置水平對齊的樣式為居中對齊; style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //設置垂直對齊的樣式為居中對齊; style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); return style; } }

InputStream通過OutputStream保存到文件

        InputStream inputStream = ...; OutputStream outputStream = null; // 打開目的輸入流,不存在則會創建 outputStream = new FileOutputStream("d:\\out.xls"); // 將輸入流is寫入文件輸出流fos中 int ch = 0; try { while ((ch = inputStream.read()) != -1) { outputStream.write(ch); } } catch (IOException e1) { e1.printStackTrace(); } finally { //關閉輸入流等(略) outputStream.close(); inputStream.close(); }

POI調用

    public static void main(String[] args) throws Exception { String[] rowsName = new String[]{"序號", "學號", "姓名", "年齡"}; List<Object[]> dataList = new ArrayList<Object[]>(); Object[] obj1 ={1,"2015202110032","牛中超",21}; Object[] obj2 = {2,"2015202110035","張子良",24}; dataList.add(obj1); dataList.add(obj2); WriteExcel ex = new WriteExcel(rowsName, dataList); InputStream inputStream = ex.export(); OutputStream outputStream = null; // 打開目的輸入流,不存在則會創建 outputStream = new FileOutputStream("d:\\out.xls"); // 將輸入流is寫入文件輸出流fos中 int ch = 0; try { while ((ch = inputStream.read()) != -1) { outputStream.write(ch); } } catch (IOException e1) { e1.printStackTrace(); } finally { //關閉輸入流等(略) outputStream.close(); inputStream.close(); } }

執行情況

enter description here


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM