public ActionForward picExcel(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response)throws Exception {
response.reset();
response.setContentType("application/vnd.ms-excel");
ServletContext context = this.getServlet().getServletContext();
try {
String path = context.getRealPath("/reports/hab/apsfa/PicStat.xls");//匯出的Excel檔案在伺服器上的輸出路徑
java.io.File reportFile1 =
new File(context.getRealPath("/reports/hab/apsfa/statPic.xls"));//此Excel檔案是伺服器上的,一個包含一張統計圖片的Excel模板
java.io.FileInputStream fos = new FileInputStream(reportFile1);//檔案輸入資料流
java.io.FileOutputStream os = new FileOutputStream(path);//檔案輸出資料流
String barName =
FileHelper.getUploadDir() + "/" + "collentResult2.jpg";//要匯出到Excel檔案中的圖片路徑
org.apache.poi.poifs.filesystem.POIFSFileSystem fs = new POIFSFileSystem(fos);
org.apache.poi.hssf.usermodel.HSSFWorkbook wb = new HSSFWorkbook(fs);
org.apache.poi.hssf.usermodel.HSSFSheet sheet = wb.getSheetAt(0);//Excel檔案中的第一個工作表
org.apache.poi.hssf.usermodel.HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
org.apache.poi.hssf.usermodel.HSSFClientAnchor anchor =new HSSFClientAnchor(
0,0,350,100,(short) 0,1,(short) 10,10);
HkSfaStatBC bc = new HkSfaStatBCImpl();//後台實作類別 --結果統計
HkSfaActivityBC abc = new HkSfaActivityBCImpl();//後台實作類別 --審計活動
String activitypk = request.getParameter("activitypk");//活動PK
List childGroups = abc.getChildgroup(activitypk);//得到此活動的所有小組
double[][] data = bc.problemCollect(activitypk);//得到統計結果,一個二維數組
org.apache.poi.hssf.usermodel.HSSFRow row = sheet.createRow(11);//在第一個工作表中建立一行(第12行)
for (int k = 0; k < childGroups.size(); k++) {
ApSfaGroupactivity activityGroup =(ApSfaGroupactivity) childGroups.get(k);//ApSfaGroupactivity為活動小組的Pojo
org.apache.poi.hssf.usermodel.HSSFCell cell = row.createCell((short) (k + 1));//在row中建立儲存格
cell.setEncoding(HSSFCell.ENCODING_UTF_16);//設定編碼
cell.setCellValue(activityGroup.getName());//給儲存格設定值
}
/* 同前一個迴圈,給Excel添加資料 */
String[] strs ={ "不適用", "符合", "整改關閉", "不符合", "未檢查", "檢查數量", "已選擇總數" };
for (int i = 12; i < strs.length + 12; i++) {
org.apache.poi.hssf.usermodel.HSSFRow row1 = sheet.createRow(i);
for (int j = 0; j < data[0].length + 1; j++) {
org.apache.poi.hssf.usermodel.HSSFCell cell = row1.createCell((short) j);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
if (j == 0) {
cell.setCellValue(strs[i - 12]);
} else {
int a = (int) data[i - 12][j - 1];
cell.setCellValue(a);
}
}
}
java.io.File jpgfile = new File(barName);//barName 為統計圖片在伺服器上的路徑
org.apache.commons.io.output.ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();//位元組輸出資料流,用來寫二進位檔案
java.awt.image.BufferedImage bufferImg = ImageIO.read(jpgfile);
javax.imageio.ImageIO.write(bufferImg, "jpg", byteArrayOut);
patriarch.createPicture(anchor,wb.addPicture(
byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));//將統計圖片添加到Excel檔案中
wb.write(os);
os.close();
net.sf.excelutils.ExcelUtils.addValue("actionServlet", this);
String config = null;
config = "/reports/hab/apsfa/PicStat.xls";
response.setHeader("Content-Disposition","attachment; filename=/"" + "PicStat.xls/"");
net.sf.excelutils.ExcelUtils.export(getServlet().getServletContext(),config,response.getOutputStream());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/*
相關Jar包和excel檔案在我的資源中有,去下載就是了,
地址為:http://download.csdn.net/source/313015
*/