在客戶端瀏覽器中點擊下載生成excel
生成excel的樣式,裏面的數據已經寫好,使用apache,poi來寫的。
1.首先是controller
/**
*下載服務結構體Excel
*
*@return
*/
@RequestMapping(value="/Downloadexcel.do")
@ResponseBody
public JsonRpcResponse structtureFileDownload(HttpServletRequest request,HttpServletResponse response){
JsonRpcResponse jsonRpcResponse = new JsonRpcResponse();
try{
//獲取服務實例編號
String serviceId = request.getParameter("serviceId");
if(StringUtils.isNotBlank(serviceId)){
//調用service層下載excel方法
Boolean flag = serviceDyService.DownloadService(serviceId,response);
if(flag){
jsonRpcResponse.setSuccess(true);
jsonRpcResponse.setMessage("實例結構體文件下載成功");
}else{
jsonRpcResponse.setSuccess(false);
jsonRpcResponse.setMessage("服務實例不存在,請輸入正確的服務實例編號");
}
}else{
jsonRpcResponse.setSuccess(false);
jsonRpcResponse.setMessage("請輸入需要下載的服務實例編號");
}
}catch(Exception e){
jsonRpcResponse.setSuccess(false);
jsonRpcResponse.setMessage("實例結構體文件下載失敗!");
e.printStackTrace();
}
return jsonRpcResponse;
}
2.然後是service
/**
*下載對應的服務結構體excel文檔
*
*@return
**/
@SuppressWarnings({"unchecked","rawtypes"})
public boolean DownService(String serviceId,HttpServletResponse response){
//通過數據元編碼查詢所有數據
String sql = "select * from .... where service_id = ‘ "+serviceId+" ‘ ";
List<Map<String,Object>> listAllData = jdbcTemplate.queryForList(sql);
for(int z = 0 ; z < listAllData.size() ; z ++ ){
Object[] oo = new Object[23];
oo[0] = listAllData.get(z).get("SERVICE_ID");
oo[1] = listAllData.get(z).get("SERVICE_NAME");
oo[2] = listAllData.get(z).get("AA");
oo[3] = listAllData.get(z).get("SOURCE_ID");
oo[4] = listAllData.get(z).get("DATA_CHINESE_NAME");
oo[5] = listAllData.get(z).get("DATA_ENGLISH_NAME");
oo[6] = listAllData.get(z).get("BUSINESS_DATA_TYPE");
oo[7] = listAllData.get(z).get("BUSINESS_DATA_LENGTH");
oo[8] = listAllData.get(z).get("BUSINESS_DATA_ACCURACY");
oo[9] = listAllData.get(z).get("BUSINESS_UNIT");
oo[10] = listAllData.get(z).get("BUSINESS_RULE");
oo[11] = listAllData.get(z).get("ENGLISH_ABBREVIATION");
oo[12] = listAllData.get(z).get("TCHNICAL_DATA_TYPE_DOMAIN");
oo[13] = listAllData.get(z).get("TCHNICAL_DATA_TYPE");
oo[14] = listAllData.get(z).get("TCHNICAL_DATA_LENGTH");
oo[15] = listAllData.get(z).get("TCHNICAL_DATA_ACCURACY");
oo[16] = listAllData.get(z).get("BB");
oo[17] = listAllData.get(z).get("CC");
oo[18] = listAllData.get(z).get("FLAG");
oo[19] = listAllData.get(z).get("GROUP_EN_NAME");
oo[20] = listAllData.get(z).get("PARENT_GROUP_CODE");
oo[21] = listAllData.get(z).get("SEQID");
oo[22] = listAllData.get(z).get("SEQ_ID");
dataList.add(oo);
}
//查詢一共有多少條數據元編碼
int countElementEncoding = jdbcTemplate.queryForObject("select count(...) from ... where ... = ...");
String title = "交易數據";
String[] rowsName = new String[]{"服務編號","服務名稱","字段類型","數據元編碼","中文名稱","英文名稱","數據類型","數據長度","數據精度","單位","業務規則","英文縮寫","數據元類型域","數據存儲類型","數據長度","數據精度","數據格式","說明","是否重復","重復組結構體名","父結構體名"," "," "};
ExportExcel ex = new ExportExcel(countElementEncoding,title,rowsName,dataList,reponse);
try{
ex.export();
}catch (Exception e1){
e1.printStackTrace();
}
if(listAllData.isEmpty()){
return false;
}
return true;
}
3.然後是ExportExcel
public class ExportExcel{
private int countElementEncoding;//數據元編碼條數
private String title;//顯示導出表的標題
private String[] rowName;//導出表的列名
private List<Object[]> dataList = new ArrayList<Object[]>();
private HttpServletResponse response;
public ExportExcel(Integer countElementEncoding,String title,String[] rowName,List<Object[]> dataList,HttpServletResponse response){
super();
this.countElementEncoding = countElementEncoding;
this.title = title;
this.rowName = rowName;
this.dataList = dataList;
this.response = response;
}
//導出數據
public void export() throws Exception{
try{
HSSFWorkbook workbook = new HSSFWorkbook();//創建工作薄對象
HSSFSheet sheet = workbook.createSheet(title);
//產生表格標題行,第一行
HSSFRow rowm1 = sheet.createRow(0);
HSSFCell cell1 = rowm1.createCell(0);
HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);
HSSFCellStyle style = this.getStyle(workbook);
//創建文本篩選框
org.apache.poi.ss.util.CellRangeAddress c1 = org.apache.poi.ss.util.CellRangeAddress.valueOf("A2:U2");
sheet.setAutoFilter(c1);
//定義所需要的列數
int columnNum = rowName.length;
HSSFRow rowRowName1 = sheet.createRow(0);
HSSFRow rowRowName2 = sheet.createRow(1);
rowRowName1.setHeight((short)(15*15));//設置第一行高度
rowRowName2.setHeight((short)(20*25));//設置第二行高度
//將列頭設置到sheet單元格中
for(int n = 0 ; n < columnNum; n ++){
//設置第一行的值
HSSFCell cellRowName1 = rowRowName1.createCell(n);//創建列頭對應個數的單元格
cellRowName1.setCellType(HSSFCell.CELL_TYPE_STRING);//創建列頭單元格的數據類型
HSSFRichTextString text1 = new HSSFRichTextString(rowName[n]);
cellRowName1.setCellValue(text1);//設置列頭單元格的值
cellRowName1.setCellStyle(columnTopStyle);//設置列頭單元格樣式
//設置第二行的值
HSSFCell cellRowName2 = rowRowName2.createCell(n);//創建列頭對應個數的單元格
cellRowName2.setCellType(HSSFCell.CELL_TYPE_STRING);//設置列頭單元格的數據類型
HSSFRichTextString text2 = new HSSFRichTextString(rowName[n]);
cellRowName2.setCellValue(text2);// 設置列頭單元格的值
cellRowName2.setCellStyle(columnTopStyle);//設置列頭單元格的樣式
}
//合並業務屬性
HSSFRow rowywsx = sheet.getRow(0);
HSSFCell cellywsx = rowywsx.getCell(6);
cellywsx.setCellValue("業務屬性");
//合並技術屬性
HSSFRow rowjssx = sheet.getRow(0);
HSSFCell celljssx = rowjssx.getCell(11);
cellywsx.setCellValue("業務屬性");
//合並重復組
HSSFRow rowcfz = sheet.getRow(0);
HSSFCell cellcfz = rowcfz.getCell(18);
cellcfz.setCellValue("重復組");
//將前三列合並
sheet.addMergeRegtion(new CellRangeAddress(2,countElementEncoding+1,0,0));
sheet.addMergeRegtion(new CellRangeAddress(2,countElementEncoding+1,1,1));
sheet.addMergeRegtion(new CellRangeAddress(2,countElementEncoding+1,2,2));
//將第一行與第二行合並
sheet.addMergeRegtion(new CellRangeAddress(0,1,0,0));
sheet.addMergeRegtion(new CellRangeAddress(0,1,1,1));
sheet.addMergeRegtion(new CellRangeAddress(0,1,2,2));
sheet.addMergeRegtion(new CellRangeAddress(0,1,3,3));
sheet.addMergeRegtion(new CellRangeAddress(0,1,4,4));
sheet.addMergeRegtion(new CellRangeAddress(0,1,5,5));
//合並業務屬性
sheet.addMergeRegion(new CellRangeAddress(0,0,6,10));
//合並技術屬性
sheet.addMergeRegion(new CellRangeAddress(0,0,11,16));
//合並說明
sheet.addMergeRegion(new CellRangeAddress(0,1,17,17));
//合並重復組
sheet.addMergeRegion(new CellRangeAddress(0,0,18,20));
//將查詢出的數據設置到sheet對應的單元格中
for(int i = 0 ; i < dataList.size() ; i ++){
Object[] obj = dataList.get(i);//遍歷每個對象
HSSFRow row = sheet.createRow(i+2);//創建所需要的行數
row.setHeight((short)(25*35));//設置第三行開始的單元格高度
for(int j = 0 ; j < obj.length ; j ++ ){
HSSFCell cell = null;//設置單元格的數據類型
cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING);
if(!" ".equals(obj[j])&& obj[j] ! = null){
cell.setCellValue(obj[j].toString());//設置單元格的值
}
cell.setCellStyle();//設置單元格樣式
}
}
if(workbook ! = null){
try{
response.setContentType("application/vnd.ms-excel;charset=uft-8");
response.setHeader("Content-Disposition","attachment;filename=""+new String("服務定義與結構體.xls".getBytes("gb2312"),"ISO8850-1"));
OutputStream out = response.getOutputStream();
workbook.write(out);
out.close();
} catch (IOException e ){
e.printStackTrace();
}
}
}catch(Exception e){
e.printStackTrace();
}
}
//設置列頭單元格樣式
public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook){
}
//設置列數據單元格樣式
public HSSFCellStyle getStyle(HSSFWorkbook workbook){
}
}
在客戶端瀏覽器中點擊下載生成excel