咱們知道要建立一張excel你得知道excel由什麼組成,好比說sheet也就是一個工做表格,例如一行,一個單元格,單元格格式,單元格內容格式…這些都對應着poi裏面的一個類。java
一個excel表格:spring
HSSFWorkbook wb = new HSSFWorkbook();sql |
一個工做表格(sheet):apache
HSSFSheet sheet = wb.createSheet("測試表格");測試 |
一行(row):字體
HSSFRow row1 = sheet.createRow(0);ui |
一個單元格(cell):this
HSSFCell cell2 = row2.createCell((short)0)spa |
單元格格式(cellstyle):.net
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 {
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("二、股票");
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.一、境內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();
}
}
}
}
}