POI匯出Excel和InputStream儲存為檔案
阿新 • • 發佈:2019-02-03
本文需要說明的兩個問題
- InputStream如何儲存到某個資料夾下
- POI生成Excel
POI操作utils類
程式碼如下。主要步驟如下:
- 建立workbook
- 建立sheet
- 生產表頭,並做相應的美化
- 將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();
}
}