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

//一个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();

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

import ins.framework.dao.GenericDaoHibernate;  

import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; import javax.annotation.Resource; import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.apache.poi.ss.usermodel.Font;
import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component; import com.reportforms.service.facade.FundDayDetailService;
@Component("fundDayDetailService")
public class FundDayDetailsServiceSpringImpl implements FundDayDetailService {
@Resource
private HibernateTemplate hibernateTemplate;
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
} @SuppressWarnings("deprecation")
public void outExcel(String fundsType, Date tradeDate, String assetsTypeCode){ HSSFWorkbook wb = new HSSFWorkbook(); //--->创建了一个excel文件
HSSFSheet sheet = wb.createSheet("理财资金报表"); //--->创建了一个工作簿
HSSFDataFormat format= wb.createDataFormat(); //--->单元格内容格式
sheet.setColumnWidth((short)3, 20* 256); //---》设置单元格宽度,因为一个单元格宽度定了那么下面多有的单元格高度都确定了所以这个方法是sheet的
sheet.setColumnWidth((short)4, 20* 256); //--->第一个参数是指哪个单元格,第二个参数是单元格的宽度
sheet.setDefaultRowHeight((short)300); // ---->有得时候你想设置统一单元格的高度,就用这个方法 //样式1
HSSFCellStyle style = wb.createCellStyle(); // 样式对象
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
//设置标题字体格式
Font font = wb.createFont();
//设置字体样式
font.setFontHeightInPoints((short)20); //--->设置字体大小
font.setFontName("Courier New"); //---》设置字体,是什么类型例如:宋体
font1.setItalic(true); //--->设置是否是加粗
style1.setFont(font1); //--->将字体格式加入到style1中
//style1.setFillForegroundColor(IndexedColors.DARK_YELLOW.getIndex());
//style1.setFillPattern(CellStyle.SOLID_FOREGROUND);设置单元格颜色
style1.setWrapText(true); //设置是否能够换行,能够换行为true
style1.setBorderBottom((short)1); //设置下划线,参数是黑线的宽度
style1.setBorderLeft((short)1); //设置左边框
style1.setBorderRight((short)1); //设置有边框
style1.setBorderTop((short)1); //设置下边框
style4.setDataFormat(format.getFormat("¥#,##0")); //--->设置为单元格内容为货币格式 style5.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00%")); //--->设置单元格内容为百分数格式 //表格第一行
HSSFRow row1 = sheet.createRow(0); //--->创建一行
// 四个参数分别是:起始行,起始列,结束行,结束列
sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 15));
row1.setHeightInPoints(25);
HSSFCell cell1 = row1.createCell((short)0); //--->创建一个单元格 cell1.setCellStyle(style);
cell1.setCellValue("总公司资金运用日报明细表(理财资金)"); //表格第二行
sheet.addMergedRegion(new Region(1,(short)0,1,(short)15));
HSSFRow row2 = sheet.createRow(1);
HSSFCell cell2 = row2.createCell((short)0);
cell2.setCellValue("报告日期:"+new Date());
cell2.setCellStyle(style2); //表格第三行
sheet.addMergedRegion(new Region(2,(short)0,2,(short)15));
HSSFRow row3 = sheet.createRow(2);
HSSFCell cell3 = row3.createCell((short)0);
cell3.setCellValue("交易日期:"+new Date());
cell3.setCellStyle(style2); //表格第四行
sheet.addMergedRegion(new Region(3, (short)0, 3, (short)2));
HSSFRow row4 = sheet.createRow(3);
row4.setHeightInPoints((short)75);
HSSFCell cell4 = row4.createCell((short)0);
HSSFCell cell4_0_1 = row4.createCell((short)1);
cell4_0_1.setCellStyle(style2);
HSSFCell cell4_0_2 = row4.createCell((short)2);
cell4_0_2.setCellStyle(style2);
cell4.setCellStyle(style1);
cell4.setCellValue("代码/品种"); HSSFCell cell4_1 = row4.createCell((short)3);
cell4_1.setCellStyle(style1);
cell4_1.setCellValue("投资类型"); HSSFCell cell4_2 = row4.createCell((short)4);
cell4_2.setCellStyle(style1);
cell4_2.setCellValue("证券账户"); HSSFCell cell4_3 = row4.createCell((short)5);
cell4_3.setCellStyle(style1);
cell4_3.setCellValue("份额\n单位:元"); HSSFCell cell4_4 = row4.createCell((short)6);
cell4_4.setCellStyle(style1);
cell4_4.setCellValue("结转总成本\n单位:元"); HSSFCell cell4_5 = row4.createCell((short)7);
cell4_5.setCellStyle(style1);
cell4_5.setCellValue("总市值\n单位:元"); HSSFCell cell4_6 = row4.createCell((short)8);
cell4_6.setCellStyle(style1);
cell4_6.setCellValue("结转成本价\n单位:元"); HSSFCell cell4_7 = row4.createCell((short)9);
cell4_7.setCellStyle(style1);
cell4_7.setCellValue("市价\n单位:元"); HSSFCell cell4_8 = row4.createCell((short)10);
cell4_8.setCellStyle(style1);
cell4_8.setCellValue("持有期收益\n单位:%"); HSSFCell cell4_9 = row4.createCell((short)11);
cell4_9.setCellStyle(style1);
cell4_9.setCellValue("总收益率(总收益/结转总成本)\n单位:%"); HSSFCell cell4_10 = row4.createCell((short)12);
cell4_10.setCellStyle(style1);
cell4_10.setCellValue("前一日涨跌幅\n单位:%"); HSSFCell cell4_11 = row4.createCell((short)13);
cell4_11.setCellStyle(style1);
cell4_11.setCellValue("盈亏\n单位:元"); HSSFCell cell4_12 = row4.createCell((short)14);
cell4_12.setCellStyle(style1);
cell4_12.setCellValue("以实现收益\n单位:元"); HSSFCell cell4_13 = row4.createCell((short)15);
cell4_13.setCellStyle(style1);
cell4_13.setCellValue("浮盈(亏)+已实现收益\n单位:元"); //第五行
sheet.addMergedRegion(new Region(4, (short)0, 4, (short)2));
HSSFRow row5 = sheet.createRow(4);
HSSFCell cell5 = row5.createCell((short)0);
HSSFCell cell5_1 = row5.createCell((short)1);
cell5_1.setCellStyle(style2);
HSSFCell cell5_2 = row5.createCell((short)2);
cell5_2.setCellStyle(style2);
cell5.setCellValue("投资资产合计");
cell5.setCellStyle(style2); //第六行
sheet.addMergedRegion(new Region(5, (short)0, 5, (short)2));
HSSFRow row6 = sheet.createRow(5);
HSSFCell cell6 = row6.createCell((short)0);
HSSFCell cell6_1 = row6.createCell((short)1);
cell6_1.setCellStyle(style2);
HSSFCell cell6_2 = row6.createCell((short)2);
cell6_2.setCellStyle(style2);
cell6.setCellValue("2、股票");
cell6.setCellStyle(style2); //第七行
sheet.addMergedRegion(new Region(6, (short)0, 6, (short)2));
HSSFRow row7 = sheet.createRow(6);
HSSFCell cell7 = row7.createCell((short)0);
HSSFCell cell7_1 = row7.createCell((short)1);
cell7_1.setCellStyle(style2);
HSSFCell cell7_2 = row7.createCell((short)2);
cell7_2.setCellStyle(style2);
cell7.setCellValue("2.1、境内A股");
cell7.setCellStyle(style2); //第八行
sheet.addMergedRegion(new Region(7, (short)0, 7, (short)2));
HSSFRow row8 = sheet.createRow(7);
HSSFCell cell8 = row8.createCell((short)0);
HSSFCell cell8_1 = row8.createCell((short)1);
cell8_1.setCellStyle(style2);
HSSFCell cell8_2 = row8.createCell((short)2);
cell8_2.setCellStyle(style2);
cell8.setCellValue("非限售股");
cell8.setCellStyle(style2); Connection conn = null;
Statement sm = null;
ResultSet rs = null;
try{
conn = hibernateTemplate.getSessionFactory().openSession().connection();
sm = conn.createStatement();
rs = sm.executeQuery(sql); int j = 0; //增加行
while(rs.next()){
HSSFRow rowN = sheet.createRow(8+j); //第9行...第n行
List<String> list = new ArrayList<String>(); //存放每一行数据
for(int i = 1 ; i <= 16 ; i++ ){
list.add(rs.getString(i));
} for(int k = 0 ; k < 16 ; k++){
if(k<5){
HSSFCell cellN = rowN.createCell((short)k);
cellN.setCellStyle(style3);
cellN.setCellValue(list.get(k));
}
if((k>=5 && k<=9)||(k>=13)){
HSSFCell cellN = rowN.createCell((short)k);
cellN.setCellStyle(style4);
cellN.setCellValue(Double.parseDouble(list.get(k)));
}
if(k>=10 && k<= 12){
HSSFCell cellN = rowN.createCell((short)k);
cellN.setCellStyle(style5);
cellN.setCellValue(Double.parseDouble(list.get(k)));
}
}
j++;
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(sm != null){
try {
sm.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} FileOutputStream fileOut = null;
try{
fileOut = new FileOutputStream("d:\\workbook.xls");
wb.write(fileOut);
//fileOut.close();
System.out.print("OK");
}catch(Exception e){
e.printStackTrace();
}
finally{
if(fileOut != null){
try {
fileOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} }

关于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. Spring JDBC数据库开发

    针对数据库操作,Spring框架提供了JdbcTemplate类. 1.Spring JDBC的配置 创建配置文件applicationContext.xml,添加如下代码: <!--配置数据源 ...

  2. Grace Hopper

    葛丽丝·穆雷·霍普(英语:Grace Murray Hopper,1906年12月9日-1992年1月1日),本姓穆雷(Murray),霍普(Hopper)为夫姓,生于美国纽约州纽约市,美国海军准将及 ...

  3. bzoj3673: 可持久化并查集 by zky&&3674: 可持久化并查集加强版

    主席树可持久化数组,还挺好YY的 然而加强版要路径压缩.. 发现压了都RE 结果看了看数据,默默的把让fx的父亲变成fy反过来让fy的父亲变成fx 搞笑啊 #include<cstdio> ...

  4. 深度 | AI芯片之智能边缘计算的崛起——实时语言翻译、图像识别、AI视频监控、无人车这些都需要终端具有较强的计算能力,从而AI芯片发展起来是必然,同时5G网络也是必然

    from:https://36kr.com/p/5103044.html 到2020年,大多数先进的ML袖珍电脑(你仍称之为手机)将有能力执行一整套任务.个人助理将变的更加智能,它是打造这种功能的切入 ...

  5. php !=和!==

    今天测试了一下!=和!== <?phpheader("Content-type: text/html; charset=utf-8"); if (1!="1&quo ...

  6. Excel 查找某列中的数据在另一列是否存在并输出其他列的数据

    最近在操作Excel文件数据导入数据库时,经常需要检查Excel中哪些数据数据库中已经存在,哪些不存在,然后再将不存在数据库中的Excel数据导入:在此过程中,经常需要操作Excel中的数据,所以.也 ...

  7. Centos上运行.net core2.0

    一.在centos7上安装.net core sdk 微软文档:https://www.microsoft.com/net/learn/get-started/linux/centos 二.直接在Ce ...

  8. jquery.slides.js

    http://slidesjs.com/#docs 一款强大的,专业的幻灯片组件,全方位对幻灯片的速度..全方位的控制: $(function(){ $("#slides").sl ...

  9. Python 中文注释报错解决方法

    代码中一旦有了中文注释便会报错. 原因 如果文件里有非ASCII字符,需要在第一行或第二行指定编码声明. 解决方法 在第一行或是第二行加入这么一句# -- coding: utf-8 -- 完美解决

  10. 决策实验(2)分水岭&哄骗实验

    转载请注明http://www.cnblogs.com/igoslly/p/6824544.html 史密斯实验II PART I 分水岭实验 两种选项 A 50%没钱,50% 45元 B 获取固定数 ...