转载自:https://blog.csdn.net/ljj_9/article/details/50395688

  1. //一个excel表格:
  2. HSSFWorkbook wb = new HSSFWorkbook();
  3.  
  4. //一个工作表格(sheet):
  5. HSSFSheet sheet = wb.createSheet("测试表格");
  6.  
  7. //一行(row):
  8. HSSFRow row1 = sheet.createRow(0);
  9.  
  10. //一个单元格(cell):
  11. HSSFCell cell2 = row2.createCell((short)0)
  12.  
  13. //单元格格式(cellstyle):
  14. HSSFCellStyle style4 = wb.createCellStyle()
  15.  
  16. //单元格内容格式()
  17. HSSFDataFormat format= wb.createDataFormat();

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

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

关于excel导出的更多相关文章

  1. [moka同学笔记]PHPexcel之excel导出和导入

    原案例来自http://www.sucaihuo.com/有修改 1.目录结构(文件不用解释,应该都可以看得懂,直接看代码)

  2. 偷懒小工具 - Excel导出公共类

    说明 最近接了一个任务,就是做一个列表的Excel导出功能.并且有很多页面都会使用这个功能. 导出的Excel大体格式如图 很简单的列表,标题加背景色,然后不同类型,显示方式不一样.对齐方式不一样.不 ...

  3. 转:POI操作Excel导出

    package com.rd.lh.util.excel; import java.beans.PropertyDescriptor; import java.io.FileOutputStream; ...

  4. TP5.0源生Excel导出

    PHPExcel类在TP5里边并不能很好的兼容,使用起来很麻烦. 不像是tp3.2那样直接import()加进来就能new,因为它里边的命名空间找不到.总是说undefined class. 如果是使 ...

  5. java反射学习之二万能EXCEL导出

    一.EXCEL导出的实现过程 假设有一个对象的集合,现在需要将此集合内的所有对象导出到EXCEL中,对象有N个属性:那么我们实现的方式是这样的: 循环这个集合,在循环集合中某个对象的所有属性,将这个对 ...

  6. Devexpress EXCEL导出

    #region EXCEL导出 /// <summary> /// EXCEL导出 /// </summary> /// <param name="saveFi ...

  7. 自己写的java excel导出工具类

    最近项目要用到excel导出功能,之前也写过类似的代码.因为这次项目中多次用到excel导出.这次长了记性整理了一下 分享给大伙 欢迎一起讨论 生成excel的主工具类: public class E ...

  8. 发邮件 和 excel导出中文文件名

    /** * 发邮件 * @param email * @param subject * @param body * @throws UnsupportedEncodingException */ pu ...

  9. asp.net(C#) Excel导出类 导出.xls文件

    ---恢复内容开始--- using Microsoft.Office.Interop.Excel; 针对office 2003需添加引用Microsoft   Excel   11.0   Obje ...

  10. Atitit.excel导出 功能解决方案 php java C#.net版总集合.doc

    Atitit.excel导出 功能解决方案 php java C#.net版总集合.docx 1.1. Excel的保存格式office2003 office2007/2010格式1 1.2. 类库选 ...

随机推荐

  1. 用SecureCRT在linux系统下载文件

    使用sz命令 说明如下: sz --helpsz version 0.12.20Usage: sz [options] file ...   or: sz [options] -{c|i} COMMA ...

  2. luogu1969 积木大赛

    题目大意 搭建一座宽度为n的大厦,大厦可以看成由n块宽度为1的积木组成,第i块积木的最终高度需要是hi. 在搭建开始之前,没有任何积木(可以看成n块高度为 0 的积木).接下来每次操作,可以选择一段连 ...

  3. [POJ 1639] Picnic Planning

    [题目链接] http://poj.org/problem?id=1639 [算法] 首先,我们可以用深度优先遍历求出1号节点去除后有几个联通块 设共有T个联通块,若T > K则无解,否则 : ...

  4. [JavaEE] 20141228_Java类文章搜集

    http://www.blogjava.net/jiangshachina 博客园java频道 Maven入门--概念与实例(原) Maven入门--较复杂的实例(原) Maven插件使用收集(原) ...

  5. sublime的常用插件

    作为一个开发者你不可能没听说过SublimeText.不过你没听说过也没关系,下面让你明白. SublimeText是一款非常精巧的文本编辑器,适合编写代码.做笔记.写文章.它用户界面十分整洁,功能非 ...

  6. BZOJ 4756 线段树合并(线段树)

    思路: 1.最裸的线段树合并 2. 我们可以观察到子树求一个东西 那我们直接DFS序好了 入队的时候统计一下有多少比他大的 出的时候统计一下 减一下 搞定~ 线段树合并代码: //By SiriusR ...

  7. LeetCode Weekly Contest 20

    1. 520. Detect Capital 题目描述的很清楚,直接写,注意:字符串长度为1的时候,大写和小写都是满足要求的,剩下的情况单独判断.还有:我感觉自己写的代码很丑,判断条件比较多,需要改进 ...

  8. Super超级ERP系统---(2)基础信息管理

    这一节我我们来了解下super系统的基础信息模块有哪些功能以及怎么实现.任何功能再强大的系统,也需要基本信息来支撑.超级erp系统的基础信息主要有供应商管理,品牌管理,分类管理,商品管理几个模块构成. ...

  9. Struts2的学习链接

    ---- Struts2的学习途径 (downpour) http://www.iteye.com/wiki/struts2/1306-struts2-way-of-learning ---- Str ...

  10. 基于 Web 的 Go 语言 IDE - Wide 1.4.0 发布!

    Wide 是什么 Wide 是一个基于 Web 的 Go 语言团队 IDE . 在线开发:打开浏览器就可以进行开发.全快捷键 智能提示:代码自动完成.查看表达式.编译反馈. Lint 实时运行:极速编 ...