跟着一起做了一個和供銷合同相關的內容,這部分內容就不粘貼了。約等於沒做什麼。
POI簡介與準備測試:
1,簡介
Apache POI是Apache軟件基金會的開放源碼函式庫,POI提供API給Java程式對Microsoft Office格式檔案讀和寫的功能。 .NET的開發人員則可以利用NPOI(POI for .NET)來存取POI的功能。
2,測試:
2.1POI操作excel的8個步驟
- 創建工作簿workbook
- 創建工作表sheet
- 創建行對象row(下表起始爲0)
- 創建一個單元格對象cell(下標起始爲0)
- 給單元格設置內容
- 設置單元格樣式,設置字體大小等
- 保存,關閉流對象
- 下載
2.2測試代碼
public class POI01Test {
@Test
public void testPoi() throws Exception{
//1.創建工作簿
Workbook wb = new XSSFWorkbook();
//2.創建工作表Sheet
Sheet sheet = wb.createSheet();
//3.創建行對象Row
Row row = sheet.createRow(3); //下標從0開始
//4.創建單元格對象 從0記數
Cell cell = row.createCell(3);
//5.設置單元格內容
cell.setCellValue("劉洋");
//6.設置單元格的樣式
CellStyle cellStyle = wb.createCellStyle();
Font font = wb.createFont();//創建字體對象
font.setFontName("葉根友行書繁");//設置字體名稱
font.setFontHeightInPoints((short)48);//設置字體大小
cellStyle.setFont(font);//樣式中添加一個字體樣式
cell.setCellStyle(cellStyle);
//7.保存,關閉流
OutputStream os = new FileOutputStream("C:/Users/liuyang19900520/Desktop/liutest.xlsx");//創建一個輸出流
wb.write(os);
os.close();
//8.下載
}
}
留下2個連接地址爲了以後用的時候參考
http://blog.csdn.net/houxuehan/article/details/50960259
http://blog.csdn.net/wushuang5566110/article/details/6415035
實現功能:
1,表頭部分編寫
注意合併單元格的編寫
sheet.addMergedRegion(new CellRangeAddress(0, 0, 1, 8));// 橫向合併單元格
2,標題的編寫
String titles[] = { "客戶", "訂單號", "貨號", "數量", "工廠", "工廠交期", "船期", "貿易條款" };
// 創建小標題的行對象
nRow = sheet.createRow(rowNo++);
nRow.setHeightInPoints(26.25f);// 設置行高
// 創建單元格對象,並設置內容 ,並設置樣式
for (String title : titles) {
nCell = nRow.createCell(cellNo++);// 創建單元格對象
nCell.setCellValue(title);// 設置內容
nCell.setCellStyle(this.title(wb));// 設置樣式
}
3,下載的編寫並說明一下工具類
DownloadUtil downUtil = new DownloadUtil();
ByteArrayOutputStream baos = new ByteArrayOutputStream();// 流 內存中的緩存區
wb.write(baos);// 將excel表格中的內容輸出到緩存
baos.close();// 刷新緩存
HttpServletResponse response = ServletActionContext.getResponse();// 得到response對象
downUtil.download(baos, response, "出貨表.xlsx");// 如果是中文,下載時可能會產生亂碼,如何解決?
3.1,download工具類主方法
public void download(ByteArrayOutputStream byteArrayOutputStream, HttpServletResponse response, String returnName) throws IOException{
response.setContentType("application/octet-stream;charset=utf-8");
returnName = response.encodeURL(new String(returnName.getBytes(),"iso8859-1")); //保存的文件名,必須和頁面編碼一致,否則亂碼
response.addHeader("Content-Disposition", "attachment;filename=" + returnName);
response.setContentLength(byteArrayOutputStream.size());
ServletOutputStream outputstream = response.getOutputStream(); //取得輸出流
byteArrayOutputStream.writeTo(outputstream); //寫到輸出流
byteArrayOutputStream.close(); //關閉
outputstream.flush(); //刷數據
}
3.2,download簡單理解
- ByteArrayOutputStream:裝着寫好excel workbook的流文件。在內存中的緩存區,不佔用服務器位置。
- HttpServletResponse:設置響應的Type和header,並且修正了中文亂碼
- String(name):中文的文件名,需要解決中文亂碼
4,完成內容
4.1,根據選中的日期查詢所有數據,得到符合要求的數據列表
4.2,遍歷數據列表對象,創建行對象,設置格式
4.3,創建單元格對象,先重置cellNo=1,設置格式,確保每行都能重第一列開始添加數據。
在這個時候需要注意一下,在添加日期數據的時候,應該format一下日期格式。
4.4,中文名亂碼修正
/**
* 下載文件時,針對不同瀏覽器,進行附件名的編碼
*
* @param filename
* 下載文件名
* @param agent
* 客戶端瀏覽器
* @return 編碼後的下載附件名
* @throws IOException
*/
public String encodeDownloadFilename(String filename, String agent) throws IOException {
if (agent.contains("Firefox")) { // 火狐瀏覽器
filename = "=?UTF-8?B?" + new BASE64Encoder().encode(filename.getBytes("utf-8")) + "?=";
} else { // IE及其他瀏覽器
filename = URLEncoder.encode(filename, "utf-8");
}
return filename;
}
在工具中適配火狐瀏覽器的中文亂碼,如果一開始導入不了這個base64的工具,需要進行一下配置
採用模板方式生成:
1,找到模板文件
/liussh_web/src/main/webapp/make/xlsprint/tOUTPRODUCT.xls
2,把創建工作簿和工作頁的工作調整爲讀取
//1.讀取工作簿
String path = ServletActionContext.getServletContext().getRealPath("/")+"/make/xlsprint/tOUTPRODUCT.xls";
System.out.println(path);
InputStream is = new FileInputStream(path);
Workbook wb = new HSSFWorkbook(is);
//2.讀取工作表
Sheet sheet = wb.getSheetAt(0);
3,獲取模板單元格的樣式並保存起來
nRow = sheet.getRow(rowNo);//讀取第三行
CellStyle customCellStyle = nRow.getCell(cellNo++).getCellStyle();
CellStyle orderNoCellStyle = nRow.getCell(cellNo++).getCellStyle();
CellStyle productNoCellStyle = nRow.getCell(cellNo++).getCellStyle();
CellStyle cNumberCellStyle = nRow.getCell(cellNo++).getCellStyle();
CellStyle factoryCellStyle = nRow.getCell(cellNo++).getCellStyle();
CellStyle deliveryPeriodCellStyle = nRow.getCell(cellNo++).getCellStyle();
CellStyle shipTimeCellStyle = nRow.getCell(cellNo++).getCellStyle();
CellStyle tradeTermsCellStyle = nRow.getCell(cellNo++).getCellStyle();
4,繪製表格的時候將樣式替換。
這部分就先告一段落吧。