生成简单的Excel表格示例

 
 
  1. package com.sanhai.nep.managerService.util;
  2. import jxl.Workbook;
  3. import jxl.format.Alignment;
  4. import jxl.write.*;
  5. import javax.servlet.http.HttpServletResponse;
  6. import java.io.File;
  7. import java.io.FileOutputStream;
  8. import java.io.OutputStream;
  9. import java.text.SimpleDateFormat;
  10. import java.util.ArrayList;
  11. import java.util.Date;
  12. import java.util.Iterator;
  13. import java.util.List;
  14. /**
  15. * Created by 胥源博 on 2016/1/26.
  16. */
  17. public class Test {
  18. public static void main(String[] args) throws Exception {
  19. arexcelUtil arexcelUtil = new arexcelUtil();
  20. List list = new ArrayList();
  21. list.add(new AccessRecord("id","111","222","333",":","444","555","666","777","888"));
  22. arexcelUtil.genarateExcel(list);
  23. }
  24. }
  25. class arexcelUtil {
  26. private ResourceBundle bundle = ResourceBundle.getBundle("exportTable");//得到exportTable.properties文件
  27. private static WritableCellFormat wcf_value; // 表格数据样式
  28. private static WritableCellFormat wcf_value_left;
  29. private static WritableCellFormat wcf_key; // 表头样式
  30. private static WritableCellFormat wcf_name_left; // 表名样式
  31. private static WritableCellFormat wcf_name_right; // 表名样式
  32. private static WritableCellFormat wcf_name_center; // 表名样式
  33. private static WritableCellFormat wcf_title; // 页名称样式
  34. private static WritableCellFormat wcf_percent_float;
  35. //根据exportTable.properties文件里的key得到Excel表格的路径
  36. private static String pathStr = bundle.getString("exportRableRoute");
  37. // 生成Excel文件
  38. public void genarateExcel(List list) throws Exception {
  39. /****** 定义表格格式start *****/
  40. WritableFont wf_key = new jxl.write.WritableFont(WritableFont.createFont("微软雅黑"), 10, WritableFont.BOLD);
  41. WritableFont wf_value = new jxl.write.WritableFont(WritableFont.createFont("微软雅黑"), 10, WritableFont.NO_BOLD);
  42. //设置单元格样式
  43. wcf_value = new WritableCellFormat(wf_value); //单元格字体样式
  44. wcf_value.setAlignment(jxl.format.Alignment.CENTRE); //单元格水平对齐样式
  45. wcf_value.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); //单元格垂直对齐样式
  46. wcf_value.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN); //单元格边框样式
  47. wcf_value_left = new WritableCellFormat(wf_value);
  48. wcf_value_left.setAlignment(jxl.format.Alignment.LEFT);
  49. wcf_value_left.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
  50. wcf_value_left.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
  51. wcf_value_left.setWrap(true);
  52. wcf_key = new WritableCellFormat(wf_key);
  53. wcf_key.setAlignment(jxl.format.Alignment.CENTRE);
  54. wcf_key.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
  55. wcf_name_left = new WritableCellFormat(wf_key);
  56. wcf_name_left.setAlignment(Alignment.LEFT);
  57. wcf_name_left.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
  58. wcf_name_right = new WritableCellFormat(wf_key);
  59. wcf_name_right.setAlignment(Alignment.LEFT);
  60. wcf_name_center = new WritableCellFormat(wf_key);
  61. wcf_name_center.setAlignment(Alignment.CENTRE);
  62. jxl.write.NumberFormat wf_percent_float = new jxl.write.NumberFormat("0.00"); //定义单元浮点数据类型
  63. wcf_percent_float = new jxl.write.WritableCellFormat(wf_value, wf_percent_float);
  64. wcf_percent_float.setAlignment(jxl.format.Alignment.CENTRE);
  65. wcf_percent_float.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
  66. wcf_percent_float.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
  67. WritableFont wf_title = new jxl.write.WritableFont(WritableFont.createFont("微软雅黑"), 24, WritableFont.NO_BOLD); //定义标题样式
  68. wcf_title = new WritableCellFormat(wf_title);
  69. wcf_title.setAlignment(Alignment.CENTRE);
  70. wcf_title.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
  71. /****** 定义表格格式end *****/
  72. //在指定的路径生成空白的xls文件
  73. SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
  74. System.out.println();
  75. String filename = pathStr + df.format(new Date()) + ".xls";
  76. File file = new File(filename);
  77. file.createNewFile();
  78. //根据传输过来的list,定义excel表格的列数
  79. //int maxRow = list.size();
  80. //int maxCol = 10;
  81. Iterator it = list.iterator();
  82. try {
  83. WritableWorkbook wb = Workbook.createWorkbook(file);
  84. //设置Excel工作簿名称
  85. WritableSheet ws = wb.createSheet("访问报表", 0);
  86. int startRowNum = 0; // 起始行
  87. int startColNum = 0; // 起始列
  88. int maxColSize = 8; // 最大列数
  89. // 设置列宽
  90. ws.setColumnView(0, 10);
  91. ws.setColumnView(1, 18);
  92. ws.setColumnView(2, 12);
  93. ws.setColumnView(3, 12);
  94. ws.setColumnView(4, 12);
  95. ws.setColumnView(5, 28);
  96. ws.setColumnView(6, 28);
  97. ws.setColumnView(7, 15);
  98. ws.addCell(new Label(startColNum, startRowNum, "来访单位记录", wcf_title));
  99. ws.mergeCells(startColNum, startRowNum, startColNum + maxColSize - 1, startRowNum); //合并单元格,合并(1,0)到(1,9)
  100. startColNum = 0;
  101. startRowNum++;
  102. //第1行,绘制表头
  103. ws.addCell(new Label(startColNum, startRowNum, "标题1", wcf_key));
  104. startColNum++;
  105. ws.addCell(new Label(startColNum, startRowNum, "标题3", wcf_key));
  106. startColNum++;
  107. ws.addCell(new Label(startColNum, startRowNum, "标题3", wcf_key));
  108. startColNum++;
  109. ws.addCell(new Label(startColNum, startRowNum, "标题4", wcf_key));
  110. startColNum++;
  111. ws.addCell(new Label(startColNum, startRowNum, "标题5", wcf_key));
  112. startColNum++;
  113. ws.addCell(new Label(startColNum, startRowNum, "标题6", wcf_key));
  114. startColNum++;
  115. ws.addCell(new Label(startColNum, startRowNum, "标题7", wcf_key));
  116. startColNum++;
  117. ws.addCell(new Label(startColNum, startRowNum, "标题8", wcf_key));
  118. startColNum++;
  119. //将行数加1,列数重置为0
  120. startRowNum++;
  121. startColNum = 0;
  122. //添加记录
  123. while (it.hasNext()) {
  124. AccessRecord ar = (AccessRecord) it.next();
  125. ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getId()), wcf_key));
  126. startColNum++;
  127. ws.addCell(new Label(startColNum, startRowNum, ar.getArOrganization(), wcf_key));
  128. startColNum++;
  129. ws.addCell(new Label(startColNum, startRowNum, ar.getArName(), wcf_key));
  130. startColNum++;
  131. ws.addCell(new Label(startColNum, startRowNum, ar.getPid(), wcf_key));
  132. startColNum++;
  133. ws.addCell(new Label(startColNum, startRowNum, ar.getPname(), wcf_key));
  134. startColNum++;
  135. ws.addCell(new Label(startColNum, startRowNum, ar.getAccessDate() + ar.getAccessTime(), wcf_key));
  136. startColNum++;
  137. ws.addCell(new Label(startColNum, startRowNum, ar.getDepartureDate() + ar.getDepartureTime(), wcf_key));
  138. startColNum++;
  139. ws.addCell(new Label(startColNum, startRowNum, ar.getContent(), wcf_key));
  140. startColNum++;
  141. //将行数加1,列数重置为0
  142. startRowNum++;
  143. startColNum = 0;
  144. }
  145. wb.write(); //生成Excel工作簿
  146. wb.close();
  147. } catch (Exception e) {
  148. e.printStackTrace();
  149. }
  150. }
  151. }
  152. class AccessRecord {
  153. private String id;
  154. private String arOrganization;
  155. private String arName;
  156. private String pid;
  157. private String pname;
  158. private String accessDate;
  159. private String accessTime;
  160. private String DepartureDate;
  161. private String DepartureTime;
  162. private String content;
  163. public AccessRecord(String id, String arOrganization,
  164. String arName, String pid, String pname,
  165. String accessDate, String accessTime,
  166. String departureDate, String departureTime, String content) {
  167. this.id = id;
  168. this.arOrganization = arOrganization;
  169. this.arName = arName;
  170. this.pid = pid;
  171. this.pname = pname;
  172. this.accessDate = accessDate;
  173. this.accessTime = accessTime;
  174. DepartureDate = DepartureDate;
  175. DepartureTime = DepartureTime;
  176. this.content = content;
  177. }
  178. public String getId() {
  179. return id;
  180. }
  181. public void setId(String id) {
  182. this.id = id;
  183. }
  184. public String getArOrganization() {
  185. return arOrganization;
  186. }
  187. public void setArOrganization(String arOrganization) {
  188. this.arOrganization = arOrganization;
  189. }
  190. public String getArName() {
  191. return arName;
  192. }
  193. public void setArName(String arName) {
  194. this.arName = arName;
  195. }
  196. public String getPid() {
  197. return pid;
  198. }
  199. public void setPid(String pid) {
  200. this.pid = pid;
  201. }
  202. public String getPname() {
  203. return pname;
  204. }
  205. public void setPname(String pname) {
  206. this.pname = pname;
  207. }
  208. public String getAccessDate() {
  209. return accessDate;
  210. }
  211. public void setAccessDate(String accessDate) {
  212. this.accessDate = accessDate;
  213. }
  214. public String getAccessTime() {
  215. return accessTime;
  216. }
  217. public void setAccessTime(String accessTime) {
  218. this.accessTime = accessTime;
  219. }
  220. public String getDepartureDate() {
  221. return DepartureDate;
  222. }
  223. public void setDepartureDate(String departureDate) {
  224. DepartureDate = departureDate;
  225. }
  226. public String getDepartureTime() {
  227. return DepartureTime;
  228. }
  229. public void setDepartureTime(String departureTime) {
  230. DepartureTime = departureTime;
  231. }
  232. public String getContent() {
  233. return content;
  234. }
  235. public void setContent(String content) {
  236. this.content = content;
  237. }
  238. }
 
 
  1. <!-- excle表格jar -->
  2. <dependency>
  3. <groupId>net.sourceforge.jexcelapi</groupId>
  4. <artifactId>jxl</artifactId>
  5. <version>2.6.10</version>
  6. </dependency>

猜你喜欢

转载自blog.csdn.net/weixin_39513166/article/details/78695302