本文關(guān)鍵詞:excel模板路徑
更多相關(guān)文章: 導(dǎo)入 導(dǎo)出 Excel 模板
HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,,擴(kuò)展名是.xls
XSSFWorkbook:是操作Excel2007的版本,擴(kuò)展名是.xlsx
以XSSFWorkbook為例:
//導(dǎo)出
public void getExcelByBoiler() {
try {
/***************沒有模板********************************/
// 第一步,創(chuàng)建一個webbook,對應(yīng)一個Excel文件
XSSFWorkbook work = new XSSFWorkbook();
// 第二步,在webbook中添加一個sheet,對應(yīng)Excel文件中的sheet
XSSFSheet sheet = work.createSheet("學(xué)生表一");
// 第三步,在sheet中添加表頭第0行,注意老版本poi對Excel的行數(shù)列數(shù)有限制short
XSSFRow row = sheet.createRow((int) 0);
// 第四步,創(chuàng)建單元格,并設(shè)置值表頭 設(shè)置表頭居中
XSSFCellStyle style = work.createCellStyle();
style.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 創(chuàng)建一個居中格式
XSSFCell cell = row.createCell((short) 0);
cell.setCellValue("學(xué)號");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("姓名");
cell.setCellStyle(style);
/*****************沒有模板END**************************************/
/******************有模板*******************************/
//
// 取得
excel模板路徑
//
String path =ServletActionContext.getServletContext().getRealPath("")
//
+ "/excelTemplate/統(tǒng)計.xlsx"; //這個是我的excel模板
//
InputStream in = new FileInputStream(new File(path));
//
//取得excel模板
//
XSSFWorkbook work = new XSSFWorkbook(in);
//
// 得到excel的第1張表
//
XSSFSheet sheet = work.getSheetAt(0);
//
// 取得第一行
//
XSSFRow row = sheet.getRow(0);
//
// 取得第一行第一列的單元格
//
XSSFCell cell = row.getCell(0);
//
// 給第一行第一列的單元格設(shè)值
//
cell.setCellValue("第一行第一列");
//
// 取得第二行
//
row = sheet.createRow(1);// 得到行
//
cell = row.createCell(0);// 得到第1個單元格
//
cell.setCellValue("第二行第一列");
// //
cell.setCellStyle(columnOne);// 填充樣式
//
//
cell = row.createCell(1);
//
cell.setCellValue("第二行第二列");
// //
cell.setCellStyle(columnOne1);// 填充樣式
//
row = sheet.createRow(2);// 得到行
//
cell = row.createCell(1);
//
cell.setCellValue("第三行第二列");
/*********************有模板END**********************************/
/*********************顯示圖片**********************************/
//獲取批注對象
// XSSFClientAnchor的參數(shù)說明:
// 參數(shù) 說明
// dx1 第1個單元格中x軸的偏移量
// dy1 第1個單元格中y軸的偏移量
// dx2
第2個單元格中x軸的偏移量
// dy2 第2個單元格中y軸的偏移量
// col1 第1個單元格的列號
// row1 第1個單元格的行號
// col2 第2個單元格的列號
// row2 第2個單元格的行號
//(int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int row2)
// 顯示圖片
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
String InputimagePath = ServletActionContext.getServletContext().getRealPath("").concat(FINAL_FOLDER_PATH).concat("20150202175722.png");
BufferedImage bufferImg = ImageIO.read(new File(InputimagePath));
ImageIO.write(bufferImg,"JPG",byteArrayOut);
//設(shè)置圖片大小,位置
XSSFClientAnchor anchor = new XSSFClientAnchor(5,0,500,122,(short) 0, 5,(short)10,15);
//創(chuàng)建
XSSFDrawing patri = sheet.createDrawingPatriarch();
patri.createPicture(anchor ,work.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
// 顯示第二張圖片
byteArrayOut = new ByteArrayOutputStream();
InputimagePath = ServletActionContext.getServletContext().getRealPath("").concat(FINAL_FOLDER_PATH).concat("20150202175754.png");
bufferImg = ImageIO.read(new File(InputimagePath));
ImageIO.write(bufferImg,"JPG",byteArrayOut);
//設(shè)置圖片大小,位置
anchor = new XSSFClientAnchor(5,0,500,122,(short) 11, 5,(short)21,15);
//創(chuàng)建
patri = sheet.createDrawingPatriarch();
patri.createPicture(anchor ,work.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
/*********************顯示圖片END**********************************/
/****************************輸出流*****************************************/
FileOutputStream fout = new FileOutputStream("E:/students.xlsx");
work.write(fout);
fout.close();
downLoadFile("E:/students.xlsx");
} catch (FileNotFoundException e) {
System.out.println("文件路徑錯誤");
e.printStackTrace();
} catch (IOException e) {
System.out.println("文件輸入流錯誤");
e.printStackTrace();
}
}
//導(dǎo)入
public void download(String path, HttpServletResponse response) throws IOException {
try {
// path是指欲下載的文件的路徑。
File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 以流的形式下載文件。
InputStream fis = new BoundedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 設(shè)置response的header
response.addHeader("content-disposition", "attachment;filename="
+ new String(filename.getBytes()));
response.addHeader("content-length", "" + file.length());
OutputStream toclient = new BufferedOutputStream(
response.getOutputStream());
response.setContentType("application/vnd.ms-excel ;charset=gb2312");
toclient.write(buffer);
toclient.flush();
toclient.close();
} catch (IIOException ex) {
ex.printStackTrace();
}
}
public void downLoadFile(String filePth) {
HttpServletResponse response =ServletActionContext.getResponse();
HttpServletRequest request =ServletActionContext.getRequest();
try {
//得到當(dāng)前路徑
//StringfilePath=request.getSession().getServletContext().getRealPath(File.separator);
File temFile = new File(filePth);
//判斷文件是否存在
if(!temFile.exists()){
response.getWriter().write("ERROR:File Not Found");
return ;
}
//處理文件名得位置(若服務(wù)器為linux和windows的處理方法不同)
String fileName =filePth.substring(filePth.lastIndexOf(File.separator)+1);
//設(shè)置頭文件,名稱和內(nèi)容的編碼不同,否則會出現(xiàn)亂碼。
response.setHeader("Content-Disposition", "attachment; filename="+new String((fileName).getBytes("gbk"),"UTF-8"));
response.setContentType("application/x-download");
OutputStream ot=response.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(temFile));
BufferedOutputStream bos = new BufferedOutputStream(ot);
byte[] buffer = new byte[4096];
int length = 0;
while((length = bis.read(buffer)) > 0){
bos.write(buffer,0,length);
}
bos.close();
bis.close();
ot.close();
} catch (Exception e) {
e.printStackTrace();
}
}
本文編號:
1329301
本文鏈接:http://sikaile.net/wenshubaike/mfmb/1329301.html