java实现excel的导入导出(poi详解)[转]

时间:2022-05-22 18:53:24

经过两天的研究,现在对excel导出有点心得了。我们使用的excel导出的jar包是poi这个阿帕奇公司的一个项目,后来被扩充了。是比较好用的excel导出工具。

下面来认识一下这个它吧。

我们知道要创建一张excel你得知道excel由什么组成,比如说sheet也就是一个工作表格,例如一行,一个单元格,单元格格式,单元格内容格式…这些都对应着poi里面的一个类。

一个excel表格:

HSSFWorkbook wb = new HSSFWorkbook();

一个工作表格(sheet):

HSSFSheet sheet = wb.createSheet("测试表格");

一行(row):

HSSFRow row1 = sheet.createRow(0);

一个单元格(cell):

HSSFCell cell2 = row2.createCell((short)0)

单元格格式(cellstyle):

HSSFCellStyle style4 = wb.createCellStyle()

单元格内容格式()

HSSFDataFormat format= wb.createDataFormat();

知道上面的基本知识后下面学起来就轻松了。我直接贴代码,这段代码会产生一个表格其实,代码长,但是很简单,一看就明白

  1. import ins.framework.dao.GenericDaoHibernate;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.sql.Connection;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9. import java.util.Date;
  10. import java.util.List;
  11. import javax.annotation.Resource;
  12. import org.apache.poi.hssf.usermodel.HSSFCell;
  13. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  14. import org.apache.poi.hssf.usermodel.HSSFDataFormat;
  15. import org.apache.poi.hssf.usermodel.HSSFRow;
  16. import org.apache.poi.hssf.usermodel.HSSFSheet;
  17. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  18. import org.apache.poi.hssf.util.Region;
  19. import org.apache.poi.ss.usermodel.Font;
  20. import org.hibernate.HibernateException;
  21. import org.hibernate.SQLQuery;
  22. import org.hibernate.Session;
  23. import org.springframework.orm.hibernate3.HibernateCallback;
  24. import org.springframework.orm.hibernate3.HibernateTemplate;
  25. import org.springframework.stereotype.Component;
  26. import com.reportforms.service.facade.FundDayDetailService;
  27. @Component("fundDayDetailService")
  28. public class FundDayDetailsServiceSpringImpl implements FundDayDetailService {
  29. @Resource
  30. private HibernateTemplate hibernateTemplate;
  31. public HibernateTemplate getHibernateTemplate() {
  32. return hibernateTemplate;
  33. }
  34. public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
  35. this.hibernateTemplate = hibernateTemplate;
  36. }
  37. @SuppressWarnings("deprecation")
  38. public void outExcel(String fundsType, Date tradeDate, String assetsTypeCode){
  39. HSSFWorkbook wb = new HSSFWorkbook();  //--->创建了一个excel文件
  40. HSSFSheet sheet = wb.createSheet("理财资金报表");   //--->创建了一个工作簿
  41. HSSFDataFormat format= wb.createDataFormat();   //--->单元格内容格式
  42. sheet.setColumnWidth((short)3, 20* 256);    //---》设置单元格宽度,因为一个单元格宽度定了那么下面多有的单元格高度都确定了所以这个方法是sheet的
  43. sheet.setColumnWidth((short)4, 20* 256);    //--->第一个参数是指哪个单元格,第二个参数是单元格的宽度
  44. sheet.setDefaultRowHeight((short)300);    // ---->有得时候你想设置统一单元格的高度,就用这个方法
  45. //样式1
  46. HSSFCellStyle style = wb.createCellStyle(); // 样式对象
  47. style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
  48. style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
  49. //设置标题字体格式
  50. Font font = wb.createFont();
  51. //设置字体样式
  52. font.setFontHeightInPoints((short)20);   //--->设置字体大小
  53. font.setFontName("Courier New");   //---》设置字体,是什么类型例如:宋体
  54. font1.setItalic(true);     //--->设置是否是加粗
  55. style1.setFont(font1);     //--->将字体格式加入到style1中
  56. //style1.setFillForegroundColor(IndexedColors.DARK_YELLOW.getIndex());
  57. //style1.setFillPattern(CellStyle.SOLID_FOREGROUND);设置单元格颜色
  58. style1.setWrapText(true);   //设置是否能够换行,能够换行为true
  59. style1.setBorderBottom((short)1);   //设置下划线,参数是黑线的宽度
  60. style1.setBorderLeft((short)1);   //设置左边框
  61. style1.setBorderRight((short)1);   //设置有边框
  62. style1.setBorderTop((short)1);   //设置下边框
  63. style4.setDataFormat(format.getFormat("¥#,##0"));    //--->设置为单元格内容为货币格式
  64. style5.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00%"));    //--->设置单元格内容为百分数格式
  65. //表格第一行
  66. HSSFRow row1 = sheet.createRow(0);   //--->创建一行
  67. // 四个参数分别是:起始行,起始列,结束行,结束列
  68. sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 15));
  69. row1.setHeightInPoints(25);
  70. HSSFCell cell1 = row1.createCell((short)0);   //--->创建一个单元格
  71. cell1.setCellStyle(style);
  72. cell1.setCellValue("总公司资金运用日报明细表(理财资金)");
  73. //表格第二行
  74. sheet.addMergedRegion(new Region(1,(short)0,1,(short)15));
  75. HSSFRow row2 = sheet.createRow(1);
  76. HSSFCell cell2 = row2.createCell((short)0);
  77. cell2.setCellValue("报告日期:"+new Date());
  78. cell2.setCellStyle(style2);
  79. //表格第三行
  80. sheet.addMergedRegion(new Region(2,(short)0,2,(short)15));
  81. HSSFRow row3 = sheet.createRow(2);
  82. HSSFCell cell3 = row3.createCell((short)0);
  83. cell3.setCellValue("交易日期:"+new Date());
  84. cell3.setCellStyle(style2);
  85. //表格第四行
  86. sheet.addMergedRegion(new Region(3, (short)0, 3, (short)2));
  87. HSSFRow row4 = sheet.createRow(3);
  88. row4.setHeightInPoints((short)75);
  89. HSSFCell cell4 = row4.createCell((short)0);
  90. HSSFCell cell4_0_1 = row4.createCell((short)1);
  91. cell4_0_1.setCellStyle(style2);
  92. HSSFCell cell4_0_2 = row4.createCell((short)2);
  93. cell4_0_2.setCellStyle(style2);
  94. cell4.setCellStyle(style1);
  95. cell4.setCellValue("代码/品种");
  96. HSSFCell cell4_1 = row4.createCell((short)3);
  97. cell4_1.setCellStyle(style1);
  98. cell4_1.setCellValue("投资类型");
  99. HSSFCell cell4_2 = row4.createCell((short)4);
  100. cell4_2.setCellStyle(style1);
  101. cell4_2.setCellValue("证券账户");
  102. HSSFCell cell4_3 = row4.createCell((short)5);
  103. cell4_3.setCellStyle(style1);
  104. cell4_3.setCellValue("份额\n单位:元");
  105. HSSFCell cell4_4 = row4.createCell((short)6);
  106. cell4_4.setCellStyle(style1);
  107. cell4_4.setCellValue("结转总成本\n单位:元");
  108. HSSFCell cell4_5 = row4.createCell((short)7);
  109. cell4_5.setCellStyle(style1);
  110. cell4_5.setCellValue("总市值\n单位:元");
  111. HSSFCell cell4_6 = row4.createCell((short)8);
  112. cell4_6.setCellStyle(style1);
  113. cell4_6.setCellValue("结转成本价\n单位:元");
  114. HSSFCell cell4_7 = row4.createCell((short)9);
  115. cell4_7.setCellStyle(style1);
  116. cell4_7.setCellValue("市价\n单位:元");
  117. HSSFCell cell4_8 = row4.createCell((short)10);
  118. cell4_8.setCellStyle(style1);
  119. cell4_8.setCellValue("持有期收益\n单位:%");
  120. HSSFCell cell4_9 = row4.createCell((short)11);
  121. cell4_9.setCellStyle(style1);
  122. cell4_9.setCellValue("总收益率(总收益/结转总成本)\n单位:%");
  123. HSSFCell cell4_10 = row4.createCell((short)12);
  124. cell4_10.setCellStyle(style1);
  125. cell4_10.setCellValue("前一日涨跌幅\n单位:%");
  126. HSSFCell cell4_11 = row4.createCell((short)13);
  127. cell4_11.setCellStyle(style1);
  128. cell4_11.setCellValue("盈亏\n单位:元");
  129. HSSFCell cell4_12 = row4.createCell((short)14);
  130. cell4_12.setCellStyle(style1);
  131. cell4_12.setCellValue("以实现收益\n单位:元");
  132. HSSFCell cell4_13 = row4.createCell((short)15);
  133. cell4_13.setCellStyle(style1);
  134. cell4_13.setCellValue("浮盈(亏)+已实现收益\n单位:元");
  135. //第五行
  136. sheet.addMergedRegion(new Region(4, (short)0, 4, (short)2));
  137. HSSFRow row5 = sheet.createRow(4);
  138. HSSFCell cell5 = row5.createCell((short)0);
  139. HSSFCell cell5_1 = row5.createCell((short)1);
  140. cell5_1.setCellStyle(style2);
  141. HSSFCell cell5_2 = row5.createCell((short)2);
  142. cell5_2.setCellStyle(style2);
  143. cell5.setCellValue("投资资产合计");
  144. cell5.setCellStyle(style2);
  145. //第六行
  146. sheet.addMergedRegion(new Region(5, (short)0, 5, (short)2));
  147. HSSFRow row6 = sheet.createRow(5);
  148. HSSFCell cell6 = row6.createCell((short)0);
  149. HSSFCell cell6_1 = row6.createCell((short)1);
  150. cell6_1.setCellStyle(style2);
  151. HSSFCell cell6_2 = row6.createCell((short)2);
  152. cell6_2.setCellStyle(style2);
  153. cell6.setCellValue("2、股票");
  154. cell6.setCellStyle(style2);
  155. //第七行
  156. sheet.addMergedRegion(new Region(6, (short)0, 6, (short)2));
  157. HSSFRow row7 = sheet.createRow(6);
  158. HSSFCell cell7 = row7.createCell((short)0);
  159. HSSFCell cell7_1 = row7.createCell((short)1);
  160. cell7_1.setCellStyle(style2);
  161. HSSFCell cell7_2 = row7.createCell((short)2);
  162. cell7_2.setCellStyle(style2);
  163. cell7.setCellValue("2.1、境内A股");
  164. cell7.setCellStyle(style2);
  165. //第八行
  166. sheet.addMergedRegion(new Region(7, (short)0, 7, (short)2));
  167. HSSFRow row8 = sheet.createRow(7);
  168. HSSFCell cell8 = row8.createCell((short)0);
  169. HSSFCell cell8_1 = row8.createCell((short)1);
  170. cell8_1.setCellStyle(style2);
  171. HSSFCell cell8_2 = row8.createCell((short)2);
  172. cell8_2.setCellStyle(style2);
  173. cell8.setCellValue("非限售股");
  174. cell8.setCellStyle(style2);
  175. Connection conn = null;
  176. Statement sm = null;
  177. ResultSet rs = null;
  178. try{
  179. conn = hibernateTemplate.getSessionFactory().openSession().connection();
  180. sm = conn.createStatement();
  181. rs = sm.executeQuery(sql);
  182. int j = 0;   //增加行
  183. while(rs.next()){
  184. HSSFRow rowN = sheet.createRow(8+j);   //第9行...第n行
  185. List<String> list = new ArrayList<String>();   //存放每一行数据
  186. for(int i = 1 ; i <= 16 ; i++ ){
  187. list.add(rs.getString(i));
  188. }
  189. for(int k = 0 ; k < 16 ; k++){
  190. if(k<5){
  191. HSSFCell cellN = rowN.createCell((short)k);
  192. cellN.setCellStyle(style3);
  193. cellN.setCellValue(list.get(k));
  194. }
  195. if((k>=5 && k<=9)||(k>=13)){
  196. HSSFCell cellN = rowN.createCell((short)k);
  197. cellN.setCellStyle(style4);
  198. cellN.setCellValue(Double.parseDouble(list.get(k)));
  199. }
  200. if(k>=10 && k<= 12){
  201. HSSFCell cellN = rowN.createCell((short)k);
  202. cellN.setCellStyle(style5);
  203. cellN.setCellValue(Double.parseDouble(list.get(k)));
  204. }
  205. }
  206. j++;
  207. }
  208. }catch(Exception e){
  209. e.printStackTrace();
  210. }finally{
  211. if(rs != null){
  212. try {
  213. rs.close();
  214. } catch (SQLException e) {
  215. // TODO Auto-generated catch block
  216. e.printStackTrace();
  217. }
  218. }
  219. if(sm != null){
  220. try {
  221. sm.close();
  222. } catch (SQLException e) {
  223. // TODO Auto-generated catch block
  224. e.printStackTrace();
  225. }
  226. }
  227. if(conn != null){
  228. try {
  229. conn.close();
  230. } catch (SQLException e) {
  231. // TODO Auto-generated catch block
  232. e.printStackTrace();
  233. }
  234. }
  235. }
  236. FileOutputStream fileOut = null;
  237. try{
  238. fileOut = new FileOutputStream("d:\\workbook.xls");
  239. wb.write(fileOut);
  240. //fileOut.close();
  241. System.out.print("OK");
  242. }catch(Exception e){
  243. e.printStackTrace();
  244. }
  245. finally{
  246. if(fileOut != null){
  247. try {
  248. fileOut.close();
  249. } catch (IOException e) {
  250. // TODO Auto-generated catch block
  251. e.printStackTrace();
  252. }
  253. }
  254. }
  255. }
  256. }