javaweb專案poi 讀取excel 並編輯輸出
FileInputStream fs=new FileInputStream( dir );
//使用POI提供的方法得到excel的資訊
POIFSFileSystem ps=new POIFSFileSystem(fs);
// 獲取excel物件
HSSFWorkbook wb=new HSSFWorkbook(ps);
// 獲取sheet頁
HSSFSheet sheet = wb.getSheetAt(0);
// 獲取行
HSSFRow row_1=sheet.getRow(0);
// 獲取 格
HSSFCell row_1_cell_13 = row_1.getCell(13);
row_1_cell_13.setCellValue("設定的值")
。。。。。編輯excel內容
//下載
OutputStream output = null;
try {
HttpServletResponse response = getRequestContext().getRes();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
// 設定頭時如果有中文需要用UrlEncode編碼一下
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition","attachment;");
output = response.getOutputStream();
wb.write(output);
output.flush();
requestContext.setRequestComplete(true);
}
catch (Exception e) {
e.printStackTrace();
AlertAjaxMessage(e.getMessage());
}
finally {
if (output != null) {
try {
wb.close();
output.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}