poi导出excel

问题描述:

poi导出一个workbook下多sheet的excel时,excel提示“此文件中的某些文本格式可能已经更改,因为它已经超出最多允许的字体数。关闭其他文档再试一次可能有用。”

原因:

创建字体样式Workbook.CreateFont() 被频繁调用;

创建单元格样式Workbook.createCellStyle(); 被频繁调用

解决:

公用时再外部创建一次,保存在session中。

相关代码:

类a.java


  1. HSSFWorkbook workbook = new HSSFWorkbook();
  2. this.setSessAttr("headStyle", expObj.getTabHeadStyle(workbook));
  3. this.setSessAttr("fontStyle", expObj.getFontStyle(workbook));
  4. this.setSessAttr("commonStyle", expObj.getTabCommStyle(workbook));

类expObj.java


  1. package com.net.mcis.action.shifts;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.OutputStream;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.Map;
  10. import org.apache.poi.hssf.usermodel.HSSFCell;
  11. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  12. import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
  13. import org.apache.poi.hssf.usermodel.HSSFFont;
  14. import org.apache.poi.hssf.usermodel.HSSFPatriarch;
  15. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  16. import org.apache.poi.hssf.usermodel.HSSFRow;
  17. import org.apache.poi.hssf.usermodel.HSSFSheet;
  18. import org.apache.poi.hssf.usermodel.HSSFSimpleShape;
  19. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  20. import org.apache.poi.hssf.util.HSSFColor;
  21. import org.apache.poi.ss.util.CellRangeAddress;
  22. import com.net.mcis.data.ho.PInHosView;
  23. import com.net.mcis.data.ho.PVisitOutView;
  24. import com.net.mcis.data.ho.PatsAmount;
  25. import com.net.mcis.data.ho.Shifts;
  26. import com.plat.core.action.BasicAction;
  27. /**
  28. * @Description: 导出交班簿excel
  29. */
  30. @SuppressWarnings({"serial"})
  31. public class ExportExcel extends BasicAction{
  32. public static void main(String[] args) {
  33. //exportExcel("1","交班簿EXCEL测试(病区A8 2018-07-18)");
  34. }
  35. @SuppressWarnings("deprecation")
  36. public boolean exportExcel(HSSFWorkbook workbook,List<Map<String,Object>> list,int day){
  37. String type = list.get(0).get("type").toString();
  38. String titStr ="";
  39. if(type.equals("30")){
  40. titStr+="历史交班簿";
  41. }else{
  42. titStr+="交班簿"+list.get(0).get("wardName").toString()+list.get(0).get("date").toString();
  43. }
  44. File file = new File("H:\poi\"+titStr+".xls");
  45. if(!file.exists()){
  46. try {
  47. file.createNewFile();
  48. } catch (IOException e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. //创建工作表
  53. HSSFSheet sheet = workbook.createSheet(day+"");
  54. //创建行
  55. HSSFRow row = sheet.createRow(0);
  56. row.setHeight((short)1000);
  57. //创建单元格
  58. HSSFCell cell = row.createCell(0);
  59. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 27));
  60. sheet.setDefaultColumnWidth((short)3.7);
  61. String str ="("+list.get(0).get("wardName").toString()+" "+list.get(0).get("date").toString()+")";
  62. cell.setCellValue("护士交班簿"+str);
  63. cell.setCellStyle((HSSFCellStyle)this.getSessAttr("headStyle"));
  64. //创建工作簿模板
  65. if(!getTabModal(workbook,sheet,list)){
  66. return false;
  67. }
  68. try {
  69. OutputStream out = new FileOutputStream(file);
  70. workbook.write(out);
  71. out.close();
  72. } catch (FileNotFoundException e) {
  73. e.printStackTrace();
  74. return false;
  75. } catch (IOException e) {
  76. e.printStackTrace();
  77. return false;
  78. }
  79. return true;
  80. }
  81. /**
  82. * @Description: 创建excel表模板与数据处理
  83. */
  84. public boolean getTabModal(HSSFWorkbook workbook,HSSFSheet sheet,List<Map<String,Object>> list){
  85. Map<String,Object> map = null;
  86. PatsAmount patAmountMorn = null;
  87. PatsAmount patAmountNoon = null;
  88. PatsAmount patAmountMoon = null;
  89. List<PInHosView> patsInHosList = null;
  90. List<PInHosView> pInHosList = null;
  91. List<PVisitOutView> pvList = null;
  92. List<Shifts> shiftObjList = null;
  93. if(null!=list&&list.size()>0){
  94. map = list.get(0);
  95. patAmountMorn = (PatsAmount) map.get("mornList");
  96. patAmountNoon = (PatsAmount) map.get("noonList");
  97. patAmountMoon = (PatsAmount) map.get("moonList");
  98. patsInHosList = (List<PInHosView>) map.get("patsInHosList");
  99. pInHosList = (List<PInHosView>) map.get("pInHosList");
  100. pvList = (List<PVisitOutView>) map.get("pvList");
  101. shiftObjList = (List<Shifts>) map.get("shiftObjList");
  102. }else{
  103. return false;
  104. }
  105. for(int i=1;i<11;i++){
  106. HSSFRow rowObj = sheet.createRow(i);
  107. rowObj.setHeight((short)300);
  108. for(int j=0;j<=27;j=j+7){
  109. HSSFCell cellObj = rowObj.createCell(j);
  110. cellObj.setCellStyle((HSSFCellStyle)this.getSessAttr("commonStyle"));
  111. if(i<7){
  112. sheet.addMergedRegion(new CellRangeAddress(i,i,j,j+6));
  113. }else{
  114. if(j==7||j==14||j==21){
  115. sheet.addMergedRegion(new CellRangeAddress(i,i,j,j+6));
  116. }
  117. }
  118. if(i==1){
  119. switch(j){
  120. case 0:
  121. cellObj.setCellValue("交班");break;
  122. case 7:
  123. cellObj.setCellValue("早班");break;
  124. case 14:
  125. cellObj.setCellValue("中班");break;
  126. case 21:
  127. cellObj.setCellValue("晚班");break;
  128. }
  129. }
  130. if(i==2){
  131. sheet.addMergedRegion(new CellRangeAddress(i,i+2,0,6));
  132. switch(j){
  133. case 0:
  134. cellObj.setCellValue("统计");break;
  135. case 7:
  136. if(null!=patAmountMorn){
  137. cellObj.setCellValue("原有人数:"+patAmountMorn.getOldAmount()+" ;入院:"+patAmountMorn.getInhosAmount()+" ;转入:"+patAmountMorn.getIndeptAmount()+" ;");break;
  138. }else{
  139. cellObj.setCellValue("原有人数:/ ;入院:/ ;转入:/ ;");break;
  140. }
  141. case 14:
  142. if(null!=patAmountNoon){
  143. cellObj.setCellValue("原有人数:"+patAmountNoon.getOldAmount()+" ;入院:"+patAmountNoon.getInhosAmount()+" ;转入:"+patAmountNoon.getIndeptAmount()+" ;");break;
  144. }else{
  145. cellObj.setCellValue("原有人数:/ ;入院:/ ;转入:/ ;");break;
  146. }
  147. case 21:
  148. if(null!=patAmountMoon){
  149. cellObj.setCellValue("原有人数:"+patAmountMoon.getOldAmount()+" ;入院:"+patAmountMoon.getInhosAmount()+" ;转入:"+patAmountMoon.getIndeptAmount()+" ;");break;
  150. }else{
  151. cellObj.setCellValue("原有人数:/ ;入院:/ ;转入:/ ;");break;
  152. }
  153. }
  154. }
  155. if(i==3){
  156. switch(j){
  157. case 7:
  158. if(null!=patAmountMorn){
  159. cellObj.setCellValue("出院:"+patAmountMorn.getOuthosAmount()+" ;转出:"+patAmountMorn.getOutdeptAmount()+" ;死亡:"+patAmountMorn.getDieAmount()+" ;病危:"+patAmountMorn.getCriticallyIll());break;
  160. }else{
  161. cellObj.setCellValue("出院:/;转出:/;死亡:/;病危:/;");break;
  162. }
  163. case 14:
  164. if(null!=patAmountNoon){
  165. cellObj.setCellValue("出院:"+patAmountNoon.getOuthosAmount()+" ;转出:"+patAmountNoon.getOutdeptAmount()+" ;死亡:"+patAmountNoon.getDieAmount()+" ;病危:"+patAmountNoon.getCriticallyIll());break;
  166. }else{
  167. cellObj.setCellValue("出院:/ ;转出:/ ;死亡:/ ;病危:/ ;");break;
  168. }
  169. case 21:
  170. if(null!=patAmountMoon){
  171. cellObj.setCellValue("出院:"+patAmountMoon.getOuthosAmount()+" ;转出:"+patAmountMoon.getOutdeptAmount()+" ;死亡:"+patAmountMoon.getDieAmount()+" ;病危:"+patAmountMoon.getCriticallyIll());break;
  172. }else{
  173. cellObj.setCellValue("出院:/ ;转出:/ ;死亡:/ ;病危:/ ;");break;
  174. }
  175. }
  176. }
  177. if(i==4){
  178. switch(j){
  179. case 7:
  180. if(null!=patAmountMorn){
  181. cellObj.setCellValue("手术:"+patAmountMorn.getOperAmount()+" ;分娩:"+patAmountMorn.getChildbirth()+" ;现有人数:"+patAmountMorn.getNowAmount()+" ;");break;
  182. }else{
  183. cellObj.setCellValue("手术:/ ;分娩:/ ;现有人数:/ ;");break;
  184. }
  185. case 14:
  186. if(null!=patAmountNoon){
  187. cellObj.setCellValue("手术:"+patAmountNoon.getOperAmount()+" ;分娩:"+patAmountNoon.getChildbirth()+" ;现有人数:"+patAmountNoon.getNowAmount()+" ;");break;
  188. }else{
  189. cellObj.setCellValue("手术:/ ;分娩:/ ;现有人数:/ ;");break;
  190. }
  191. case 21:
  192. if(null!=patAmountMoon){
  193. cellObj.setCellValue("手术:"+patAmountMoon.getOperAmount()+" ;分娩:"+patAmountMoon.getChildbirth()+" ;现有人数:"+patAmountMoon.getNowAmount()+" ;");break;
  194. }else{
  195. cellObj.setCellValue("手术:/ ;分娩:/ ;现有人数:/ ;");break;
  196. }
  197. }
  198. }
  199. if(i==5||i==8){
  200. sheet.addMergedRegion(new CellRangeAddress(i,i+2,0,6));
  201. if(i==5){
  202. switch(j){
  203. case 0:
  204. cellObj.setCellValue("今日入院");break;
  205. case 7:
  206. sheet.addMergedRegion(new CellRangeAddress(i,i+2,7,27));
  207. String str = "";
  208. if(null!=pInHosList&&pInHosList.size()>0){
  209. for(PInHosView pInHosObj:pInHosList){
  210. if(null!=pInHosObj.getDiagShort()&&!"".equals(pInHosObj.getDiagShort())){
  211. str+=pInHosObj.getBedNo().toString()+"-"+pInHosObj.getPNam()+"("+pInHosObj.getDiagShort()+");";
  212. }else{
  213. str+=pInHosObj.getBedNo().toString()+"-"+pInHosObj.getPNam()+";";
  214. }
  215. }
  216. }else{
  217. str = "今日暂无入院";
  218. }
  219. cellObj.setCellValue(str);break;
  220. }
  221. }else{
  222. switch(j){
  223. case 0:
  224. cellObj.setCellValue("今日出院");break;
  225. case 7:
  226. sheet.addMergedRegion(new CellRangeAddress(i,i+2,7,27));
  227. String str = "";
  228. if(null!=pvList&&pvList.size()>0){
  229. for(PVisitOutView pvObj:pvList){
  230. str+=pvObj.getBedShow()+"-"+pvObj.getPNam()+" ;";
  231. }
  232. }else{
  233. str = "今日暂无出院";
  234. }
  235. cellObj.setCellValue(str);break;
  236. }
  237. }
  238. }
  239. }
  240. }
  241. if(null!=patsInHosList&&patsInHosList.size()>0){
  242. int length = 3*patsInHosList.size()+11;
  243. sheet.addMergedRegion(new CellRangeAddress(11,length-1,0,1));
  244. for(int i=11;i<length;i++){
  245. if((i-11)%3==0){
  246. sheet.addMergedRegion(new CellRangeAddress(i,i+2,2,6));
  247. for(int j=0;j<=27;j++){
  248. if(j==7||j==14||j==21){
  249. sheet.addMergedRegion(new CellRangeAddress(i,i,j,j+6));
  250. sheet.addMergedRegion(new CellRangeAddress(i+1,i+2,j,j+6));
  251. }
  252. }
  253. HSSFRow rowObj = sheet.createRow(i);
  254. HSSFRow shiftRowObj = sheet.createRow(i+1);
  255. HSSFRow shiftRowObj2 = sheet.createRow(i+2);
  256. rowObj.setHeight((short)300);
  257. shiftRowObj.setHeight((short)300);
  258. shiftRowObj2.setHeight((short)300);
  259. HSSFCell shiftCellObj = null;
  260. HSSFCell descCellObj = null;
  261. HSSFCell cellObj = rowObj.createCell(2);
  262. if(i==11){
  263. HSSFCell cellObj2 = rowObj.createCell(0);
  264. cellObj2.setCellStyle(getFontStyle(workbook));//字体红色
  265. cellObj2.setCellValue("手术");
  266. }
  267. PInHosView pat = null;
  268. List<Shifts> objShift = new ArrayList<Shifts>();
  269. int num = (i-11)/3;
  270. pat = patsInHosList.get(num);
  271. String infoStr = pat.getBedShow()+"-"+pat.getPNam()+""+pat.getDiagShort();
  272. cellObj.setCellStyle((HSSFCellStyle)this.getSessAttr("commonStyle"));//getTopStyle(workbook)
  273. cellObj.setCellValue(new HSSFRichTextString(infoStr));
  274. if(null!=shiftObjList&&shiftObjList.size()>0){
  275. for(int k=0;k<shiftObjList.size();k++){
  276. Shifts shiftTempObj = shiftObjList.get(k);
  277. if(pat.getPid().equals(shiftTempObj.getPatientId())){
  278. objShift.add(shiftTempObj);
  279. }
  280. }
  281. }
  282. if(null!=objShift&&objShift.size()>0){
  283. for(int t=0;t<objShift.size();t++){
  284. Shifts shiftTempObj = objShift.get(t);
  285. int tempShift = shiftTempObj.getShift();
  286. if(1==tempShift){
  287. shiftCellObj = rowObj.createCell(7);
  288. shiftCellObj.setCellValue(shiftTempObj.getTimePoint()+" T:"+shiftTempObj.getTemperature()+" P:"+shiftTempObj.getPulse()+" R:"+shiftTempObj.getRespire());
  289. descCellObj = shiftRowObj.createCell(7);
  290. descCellObj.setCellValue(shiftTempObj.getShiftNote());
  291. }else if(2==tempShift){
  292. shiftCellObj = rowObj.createCell(14);
  293. shiftCellObj.setCellValue(shiftTempObj.getTimePoint()+" T:"+shiftTempObj.getTemperature()+" P:"+shiftTempObj.getPulse()+" R:"+shiftTempObj.getRespire());
  294. descCellObj = shiftRowObj.createCell(14);
  295. descCellObj.setCellValue(shiftTempObj.getShiftNote());
  296. }else if(3==tempShift){
  297. shiftCellObj = rowObj.createCell(21);
  298. shiftCellObj.setCellValue(shiftTempObj.getTimePoint()+" T:"+shiftTempObj.getTemperature()+" P:"+shiftTempObj.getPulse()+" R:"+shiftTempObj.getRespire());
  299. descCellObj = shiftRowObj.createCell(21);
  300. descCellObj.setCellValue(shiftTempObj.getShiftNote());
  301. }
  302. shiftCellObj.setCellStyle((HSSFCellStyle)this.getSessAttr("commonStyle"));
  303. descCellObj.setCellStyle((HSSFCellStyle)this.getSessAttr("commonStyle"));
  304. }
  305. }
  306. }
  307. }
  308. }
  309. return true;
  310. }
  311. /**
  312. * @Description: 画斜线
  313. */
  314. private void drawLine(HSSFSheet sheet) {
  315. HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
  316. HSSFClientAnchor a = new HSSFClientAnchor(0, 0, 1023, 255, (short)0, 2, (short)3, 4);
  317. HSSFSimpleShape shape1 = patriarch.createSimpleShape(a);
  318. shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
  319. shape1.setLineStyle(HSSFSimpleShape.LINESTYLE_SOLID) ;
  320. }
  321. /**
  322. * @Description 表头样式
  323. * @param workbook 工作簿
  324. * @param cell 表头
  325. */
  326. public HSSFCellStyle getTabHeadStyle(HSSFWorkbook workbook){
  327. HSSFFont font = workbook.createFont();
  328. HSSFCellStyle style = workbook.createCellStyle();
  329. style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  330. style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  331. font.setFontHeightInPoints((short)16);
  332. font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  333. font.setFontName("songti");
  334. style.setFont(font);
  335. return style;
  336. }
  337. /**
  338. * @Description 公共样式
  339. * @param workbook 工作簿
  340. */
  341. public HSSFCellStyle getTabCommStyle(HSSFWorkbook workbook){
  342. HSSFFont font = workbook.createFont();
  343. HSSFCellStyle style = workbook.createCellStyle();
  344. style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  345. style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  346. font.setFontHeightInPoints((short)8);
  347. font.setFontName("songti");
  348. style.setFont(font);
  349. style.setWrapText(true);
  350. return style;
  351. }
  352. /**
  353. * @Description: 设置手术样式
  354. */
  355. public HSSFCellStyle getFontStyle(HSSFWorkbook workbook){
  356. HSSFFont font = workbook.createFont();
  357. HSSFCellStyle style = workbook.createCellStyle();
  358. font.setColor(HSSFColor.RED.index);
  359. font.setFontHeightInPoints((short)8);
  360. font.setFontName("songti");
  361. style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  362. style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  363. style.setFont(font);
  364. return style;
  365. }
  366. }

poi导出excel

相关推荐