1.如何經過元數據拿到數據庫的信息?
2.如何用Java生成Excel表?
3.將數據庫中的表導出生成Excel案例java
元數據:描述數據的數據mysql
Java中使用元數據的兩個方法sql
DatabaseMetaData 的使用學習數據庫
@Test// DatabaseMetaData 經過鏈接能夠拿到的信息:數據庫軟件,全部數據庫名,全部數據庫裏面的表名 public void DatabaseMetaData_Demo() throws Exception{ // 本身寫的工具包來得到數據庫鏈接 Connection con = ConnUtils4.getConn(); //DatabaseMetaData 經過鏈接得到 DatabaseMetaData dbmt = con.getMetaData(); // 數據庫軟件名 System.out.println(" 數據庫軟件名:"+dbmt.getDatabaseProductName()); // 拿到全部數據庫名字 ResultSet rs =dbmt.getCatalogs(); List<String> tablenames = new ArrayList<String>(); while(rs.next()){ String tabname=rs.getString("TABLE_CAT"); tablenames.add(tabname); System.out.println("數據庫名字:"+tabname); } System.out.println("--------------");; //拿到某個數據庫李曼全部的表名---能夠指定表的類型 rs = dbmt.getTables("ake", "ake", null, new String[]{"TABLE","VIEW"}); while(rs.next()){ System.out.println("數據庫ake裏的表名:"+rs.getString("TABL````` _NAME")); } }markdown
ResultSetMetaData的使用學習app
@Test// ResultSetMetaData 拿到的表結構信息:得到表的列數目 類型和屬性 public void ResultSetMetaData_Demo2() throws Exception{ // 本身寫的工具包來得到數據庫鏈接 Connection con = ConnUtils4.getConn(); String sql = "select * from ake.book"; Statement st = con.createStatement(); ResultSet rs = st.executeQuery(sql); // ResultSetMetaData 經過 查詢的返回集獲取 ResultSetMetaData rsmt = rs.getMetaData(); //得到表的列數 int n =rsmt.getColumnCount(); //類型---某一列 // getColumnTypeName:INT //某醫療的名字 // getColumnName:id //某一列的長度 // getColumnDisplaySize:11 for(int i=1;i<n;i++){ System.out.println(rsmt.getTableName(i)+"表的第"+i+"列描述信息"); System.out.println("getColumnDisplaySize:"+rsmt.getColumnDisplaySize(i)); System.out.println("getColumnLabel:"+rsmt.getColumnLabel(i)); System.out.println("getColumnName:"+rsmt.getColumnName(i)); System.out.println("getColumnType:"+rsmt.getColumnType(i)); System.out.println("getColumnTypeName:"+rsmt.getColumnTypeName(i)); System.out.println("getPrecision:"+rsmt.getPrecision(i)); System.out.println("getScale:"+rsmt.getScale(i)); System.out.println("getSchemaName:"+rsmt.getSchemaName(i)); System.out.println("------------"); } con.close(); }工具
拿出ake表裏面全部的內容~~~~post
// 拿出ake表裏面全部的內容~~~~ public static void main(String[] args) throws Exception{ Connection con = ConnUtils4.getConn(); System.out.println(con); DatabaseMetaData dbmt = con.getMetaData(); //拿到全部的ake全部表名 ResultSet rs =dbmt.getTables("ake", "ake", null, new String[]{"TABLE","VIEW"}); List<String> tablenames = new ArrayList<String>(); while(rs.next()){ String tablename = rs.getString("TABLE_NAME"); tablenames.add(tablename); } for(String tablename:tablenames){ System.out.println(tablename+"表:"); if(tablename.equals("img")){ continue; } String sql = "select * from ake."+tablename; Statement st = con.createStatement(); ResultSet RS = st.executeQuery(sql); ResultSetMetaData rsmt = RS.getMetaData(); // 拿到列數 int colnums = rsmt.getColumnCount(); for(int i=1;i<=colnums;i++){ //拿到表頭信息 String colName = rsmt.getColumnName(i); System.out.print(colName+"\t"); } System.out.println(); while(RS.next()){ for(int i=1;i<=colnums;i++){ //拿到表信息 System.out.print( RS.getString(i)+"\t"); } System.out.println(); } } con.close(); }學習
我把那到的表格信息輸出
ui
須要一個插件工具包
@Test public void Workbook_demo() throws Exception{ // 創建一個工做表--至關於一個數據庫 Workbook book = new HSSFWorkbook(); // 數據庫中的一個表 Sheet sheet1 =book.createSheet("表1"); // 行 Row row =sheet1.createRow(4); // 單元格 Cell cell = row.createCell(3); // 寫入數據 cell.setCellValue("經過java寫的Excel"); // 保存到銀盤 book.write( new FileOutputStream("d:a/a.xls")); }
public static void main(String[] args) throws Exception { //把數據庫裏全部的信息導入到Excel表中~ Connection con = ConnUtils4.getConn(); DatabaseMetaData dbmt = con.getMetaData(); //要經過 DatabaseMetaData 拿到全部數據庫的名字 List<String> Database_Names = new ArrayList<String>(); ResultSet rs =dbmt.getCatalogs(); while(rs.next()){ Database_Names.add( rs.getString("TABLE_CAT")); } //DatabaseMetaData 拿到全部數據表名 int m = 0; for(String Database_Name:Database_Names){ if(!Database_Name.equals("ake")){ continue; } // if(m++>=3){ // break; // } // 一個數據庫對於一個 Excel文檔~ Workbook book = new HSSFWorkbook(); rs = dbmt.getTables(Database_Name, Database_Name, null, new String[]{"TABLE","VIEW"}); //封裝全部表名 List<String> Table_Names = new ArrayList<String>(); while(rs.next()){ Table_Names.add( rs.getString("TABLE_NAME")); } for(String Table_Name:Table_Names){ if("img".equals(Table_Name) ||"note".equals(Table_Name) ){ // img爲二進制文件導入會出錯 continue; } //建立一個表 Sheet sheet = book.createSheet(Table_Name); Statement st = con.createStatement(); String sql = "select * from "+Database_Name+"."+Table_Name; // System.out.println("sql:"+sql); rs = st.executeQuery(sql); //設置表頭信息 Row row1 = sheet.createRow(0); ResultSetMetaData rsmd = rs.getMetaData(); int colnum = rsmd.getColumnCount(); for(int i=1;i<=colnum;i++){ String name = rsmd.getColumnName(i); Cell cell = row1.createCell(i-1); cell.setCellValue(name); // System.out.print(name+"\t"); } // System.out.println(); //設置表格信息 int idx = 1; while(rs.next()){ Row row = sheet.createRow(idx++); for(int i=1;i<=colnum;i++){ String str = rs.getString(i); // System.out.print(str+"\t"); Cell cell = row.createCell(i-1); cell.setCellValue(str); } // System.out.println(); } } book.write( new FileOutputStream( "d:a/"+Database_Name+".xls")); } }
執行結果就成功啦!
/** * excel * @param request * @param resp * @throws UnsupportedEncodingException */ @RequestMapping(value = "/exportReceiptReport", method = RequestMethod.GET) @ResponseBody public void exportReceiptReport(HttpServletRequest request, HttpServletResponse response,JyHTMLpurchaseinfoApiForm form) throws IOException { HSSFWorkbook wb = new HSSFWorkbook(); request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("application/x-download"); Date d = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String now = sdf.format(d); String fileName = "採購"+now+".xls"; fileName = URLEncoder.encode(fileName, "UTF-8"); response.addHeader("Content-Disposition", "attachment;filename=" + fileName); HSSFSheet sheet = wb.createSheet("sheet1"); sheet.setDefaultRowHeight((short) (2 * 256)); HSSFFont font = wb.createFont(); font.setFontName("宋體"); font.setFontHeightInPoints((short) 16); HSSFRow row = sheet.createRow((int) 0); sheet.createRow((int) 1); sheet.createRow((int) 2); sheet.createRow((int) 3); sheet.createRow((int) 4); sheet.createRow((int) 5); sheet.createRow((int) 6); sheet.createRow((int) 7); HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFCell cell = row.createCell(0); cell.setCellValue("收貨"); cell.setCellStyle(style); cell = row.createCell(1); cell.setCellValue("來源"); cell.setCellStyle(style); cell = row.createCell(2); cell.setCellValue("入庫"); cell.setCellStyle(style); cell = row.createCell(3); cell.setCellValue("名稱"); cell.setCellStyle(style); cell = row.createCell(4); cell.setCellValue("商品名稱"); cell.setCellStyle(style); cell = row.createCell(5); cell.setCellValue("單位"); cell.setCellStyle(style); cell = row.createCell(6); cell.setCellValue("規格"); cell.setCellStyle(style); cell = row.createCell(7); cell.setCellValue("數量"); cell.setCellStyle(style); cell = row.createCell(8); cell.setCellValue("單價"); cell.setCellStyle(style); cell = row.createCell(9); cell.setCellValue("金額"); cell.setCellStyle(style); cell = row.createCell(10); cell.setCellValue("用戶名稱"); cell.setCellStyle(style); List list = SF.JYPURCHASEINFO_SERVICE.exportReceiptReport(form); for (int i = 0; i < list.size(); i++) { HSSFRow row1 = sheet.createRow(i+1); Map<String, Object> map = (Map<String,Object>)list.get(i); row1.createCell(0).setCellValue((map.get("receipt_no") != null ? map.get("receipt_no") : "").toString()); row1.createCell(1).setCellValue((map.get("purchase_no") != null ? map.get("purchase_no") : "").toString()); row1.createCell(2).setCellValue((map.get("value") != null ? map.get("value") : "").toString()); row1.createCell(3).setCellValue((map.get("supplier_name") != null ? map.get("supplier_name") : "").toString()); row1.createCell(4).setCellValue((map.get("packing_name") != null ? map.get("packing_name") : "").toString()); row1.createCell(5).setCellValue((map.get("purchase_unit") != null ? map.get("purchase_unit") : "").toString()); row1.createCell(6).setCellValue((map.get("packing_spec") != null ? map.get("packing_spec") : "").toString()); row1.createCell(7).setCellValue((map.get("boxcount") != null ? map.get("boxcount") : "").toString()+ (map.get("packing_unit") != null ? map.get("packing_unit") : "").toString()+ (map.get("pingCount") != null ? map.get("pingCount") : "").toString()+ (map.get("item_unit") != null ? map.get("item_unit") : "").toString()); row1.createCell(9).setCellValue((map.get("purchase_price") != null ? map.get("purchase_price") : "").toString()); row1.createCell(10).setCellValue((map.get("name") != null ? map.get("name") : "").toString()); }
//sheet.autoSizeColumn((short)0); //調整第一列寬度. try { OutputStream out = response.getOutputStream(); wb.write(out); out.flush(); out.close(); } catch (ServiceException e) { logger.info ("ServiceException=====導出excel異常====" + e); e.printStackTrace(); } catch (Exception e1) { logger.info ("Exception=====導出excel異常====" + e1); e1.printStackTrace(); } }
多個excel:
/** * 費用結算清單導出: */ @RequestMapping(value = "/exportDealerCosts", method = RequestMethod.GET) @ResponseBody public void exportDealerCosts(HttpServletRequest request, HttpServletResponse response,JyDealerHomeform form) throws IOException { HSSFWorkbook wb = new HSSFWorkbook(); request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("application/x-download"); Date d = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String now = sdf.format(d); String fileName = "費用結算清單"+now+".xls"; fileName = URLEncoder.encode(fileName, "UTF-8"); response.addHeader("Content-Disposition", "attachment;filename=" + fileName); //合計 HSSFSheet sheet = wb.createSheet("費用結算清單"); sheet.setDefaultRowHeight((short) (2 * 256)); HSSFFont font = wb.createFont(); font.setFontName("宋體"); font.setFontHeightInPoints((short) 16); HSSFRow row = sheet.createRow((int) 0); sheet.createRow((int) 1); sheet.createRow((int) 2); sheet.createRow((int) 3); sheet.createRow((int) 4); sheet.createRow((int) 5); sheet.createRow((int) 6); sheet.createRow((int) 7); HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFCell cell = row.createCell(0); cell.setCellValue("公司名稱"); cell.setCellStyle(style); cell = row.createCell(1); cell.setCellValue("倉儲費"); cell.setCellStyle(style); cell = row.createCell(2); cell.setCellValue("配送費"); cell.setCellStyle(style); cell = row.createCell(3); cell.setCellValue("回空配送費"); cell.setCellStyle(style); cell = row.createCell(4); cell.setCellValue("退貨配偶費"); cell.setCellStyle(style); cell = row.createCell(5); cell.setCellValue("入出庫費"); cell.setCellStyle(style); List dealerCosts = SF.JYDEALER_SERVICE.dealerCosts(form); for (int i = 0; i < dealerCosts.size(); i++) { HSSFRow row1 = sheet.createRow(i+1); Map<String, Object> map = (Map<String,Object>)dealerCosts.get(i); row1.createCell(0).setCellValue((map.get("name") != null ? map.get("name") : "").toString()); row1.createCell(1).setCellValue((map.get("cc_fee") != null ? map.get("cc_fee") : "").toString()); row1.createCell(2).setCellValue((map.get("ps_fee") != null ? map.get("ps_fee") : "").toString()); row1.createCell(3).setCellValue((map.get("hkps_fee") != null ? map.get("hkps_fee") : "").toString()); row1.createCell(4).setCellValue((map.get("thps_fee") != null ? map.get("thps_fee") : "").toString()); row1.createCell(5).setCellValue((map.get("rc_fee") != null ? map.get("rc_fee") : "").toString()); } //倉儲費 HSSFSheet sheet2 = wb.createSheet("倉儲費明細"); sheet2.setDefaultRowHeight((short) (2 * 256)); HSSFRow row2 = sheet2.createRow((int) 0); sheet2.createRow((int) 1); sheet2.createRow((int) 2); sheet2.createRow((int) 3); sheet2.createRow((int) 4); sheet2.createRow((int) 5); sheet2.createRow((int) 6); sheet2.createRow((int) 7); HSSFCell cell2 = row2.createCell(0); cell2.setCellValue("日期"); cell2.setCellStyle(style); cell2 = row2.createCell(1); cell2.setCellValue("公司名稱"); cell2.setCellStyle(style); cell2 = row2.createCell(2); cell2.setCellValue("商品類型"); cell2.setCellStyle(style); cell2 = row2.createCell(3); cell2.setCellValue("商品名稱"); cell2.setCellStyle(style); cell2 = row2.createCell(4); cell2.setCellValue("庫存件數"); cell2.setCellStyle(style); cell2 = row2.createCell(5); cell2.setCellValue("總重量"); cell2.setCellStyle(style); cell2 = row2.createCell(6); cell2.setCellValue("倉儲費"); cell2.setCellStyle(style); List dealerStorageFee = SF.JYDEALER_SERVICE.dealerStorageFee(form); for (int i = 0; i < dealerStorageFee.size(); i++) { HSSFRow row1 = sheet2.createRow(i+1); Map<String, Object> map = (Map<String,Object>)dealerStorageFee.get(i); row1.createCell(0).setCellValue((map.get("calendarDate") != null ? map.get("calendarDate") : "").toString()); row1.createCell(1).setCellValue((map.get("dealerName") != null ? map.get("dealerName") : "").toString()); row1.createCell(2).setCellValue((map.get("itemType") != null ? map.get("itemType") : "").toString()); row1.createCell(3).setCellValue((map.get("packingName") != null ? map.get("packingName") : "").toString()); row1.createCell(4).setCellValue((map.get("packingQty") != null ? map.get("packingQty") : "").toString()); row1.createCell(5).setCellValue((map.get("totalWeight") != null ? map.get("totalWeight") : "").toString()); row1.createCell(6).setCellValue((map.get("totalAmount") != null ? map.get("totalAmount") : "").toString()); } //配送費 HSSFSheet sheet3 = wb.createSheet("配送費明細"); sheet3.setDefaultRowHeight((short) (2 * 256)); HSSFRow row3 = sheet3.createRow((int) 0); sheet3.createRow((int) 1); sheet3.createRow((int) 2); sheet3.createRow((int) 3); sheet3.createRow((int) 4); sheet3.createRow((int) 5); sheet3.createRow((int) 6); sheet3.createRow((int) 7); HSSFCell cell3 = row3.createCell(0); cell3.setCellValue("日期"); cell3.setCellStyle(style); cell3 = row3.createCell(1); cell3.setCellValue("公司名稱"); cell3.setCellStyle(style); cell3 = row3.createCell(2); cell3.setCellValue("費用區分"); cell3.setCellStyle(style); cell3 = row3.createCell(3); cell3.setCellValue("費項"); cell3.setCellStyle(style); cell3 = row3.createCell(4); cell3.setCellValue("金額"); cell3.setCellStyle(style); cell3 = row3.createCell(5); cell3.setCellValue("客戶"); cell3.setCellStyle(style); cell3 = row3.createCell(6); cell3.setCellValue("結算對象"); cell3.setCellStyle(style); cell3 = row3.createCell(7); cell3.setCellValue("訂單編號"); cell3.setCellStyle(style); cell3 = row3.createCell(8); cell3.setCellValue("送貨件數"); cell3.setCellStyle(style); cell3 = row3.createCell(9); cell3.setCellValue("總重量"); cell3.setCellStyle(style); cell3 = row3.createCell(10); cell3.setCellValue("送貨單號"); cell3.setCellStyle(style); cell3 = row3.createCell(11); cell3.setCellValue("配送方式"); cell3.setCellStyle(style); List dealerDeliverFee = SF.JYDEALER_SERVICE.dealerDeliverFee(form); for (int i = 0; i < dealerDeliverFee.size(); i++) { HSSFRow row1 = sheet3.createRow(i+1); Map<String, Object> map = (Map<String,Object>)dealerDeliverFee.get(i); row1.createCell(0).setCellValue((map.get("confirm_date") != null ? map.get("confirm_date") : "").toString()); row1.createCell(1).setCellValue((map.get("name") != null ? map.get("name") : "").toString()); row1.createCell(2).setCellValue((map.get("fee_type_name") != null ? map.get("fee_type_name") : "").toString()); row1.createCell(3).setCellValue((map.get("calculate_subject") != null ? map.get("calculate_subject") : "").toString()); row1.createCell(4).setCellValue((map.get("out_amount") != null ? map.get("out_amount") : "").toString()); row1.createCell(5).setCellValue((map.get("customer_name") != null ? map.get("customer_name") : "").toString()); row1.createCell(6).setCellValue((map.get("source_type_name") != null ? map.get("source_type_name") : "").toString()); row1.createCell(7).setCellValue((map.get("source_order_no") != null ? map.get("source_order_no") : "").toString()); row1.createCell(8).setCellValue((map.get("box_count") != null ? map.get("box_count") : "").toString()); row1.createCell(9).setCellValue((map.get("weight") != null ? map.get("weight") : "").toString()); row1.createCell(10).setCellValue((map.get("deliver_no") != null ? map.get("deliver_no") : "").toString()); row1.createCell(11).setCellValue((map.get("value") != null ? map.get("value") : "").toString()); } //回空配送費 HSSFSheet sheet4 = wb.createSheet("回空配送費明細"); sheet4.setDefaultRowHeight((short) (2 * 256)); HSSFRow row4 = sheet4.createRow((int) 0); sheet4.createRow((int) 1); sheet4.createRow((int) 2); sheet4.createRow((int) 3); sheet4.createRow((int) 4); sheet4.createRow((int) 5); sheet4.createRow((int) 6); sheet4.createRow((int) 7); HSSFCell cell4 = row4.createCell(0); cell4.setCellValue("日期"); cell4.setCellStyle(style); cell4 = row4.createCell(1); cell4.setCellValue("公司名稱"); cell4.setCellStyle(style); cell4 = row4.createCell(2); cell4.setCellValue("回空品名稱"); cell4.setCellStyle(style); cell4 = row4.createCell(3); cell4.setCellValue("費用區分"); cell4.setCellStyle(style); cell4 = row4.createCell(4); cell4.setCellValue("費項"); cell4.setCellStyle(style); cell4 = row4.createCell(5); cell4.setCellValue("金額"); cell4.setCellStyle(style); cell4 = row4.createCell(6); cell4.setCellValue("收貨單號"); cell4.setCellStyle(style); cell4 = row4.createCell(7); cell4.setCellValue("客戶"); cell4.setCellStyle(style); cell4 = row4.createCell(8); cell4.setCellValue("結算對象"); cell4.setCellStyle(style); cell4 = row4.createCell(9); cell4.setCellValue("銷售單號"); cell4.setCellStyle(style); cell4 = row4.createCell(10); cell4.setCellValue("回空件數"); cell4.setCellStyle(style); cell4 = row4.createCell(11); cell4.setCellValue("送貨單號"); cell4.setCellStyle(style); List dealerBackDeliverFee = SF.JYDEALER_SERVICE.dealerBackDeliverFee(form); for (int i = 0; i < dealerBackDeliverFee.size(); i++) { HSSFRow row1 = sheet4.createRow(i+1); Map<String, Object> map = (Map<String,Object>)dealerBackDeliverFee.get(i); row1.createCell(0).setCellValue((map.get("confirm_date") != null ? map.get("confirm_date") : "").toString()); row1.createCell(1).setCellValue((map.get("name") != null ? map.get("name") : "").toString()); row1.createCell(2).setCellValue((map.get("item_name") != null ? map.get("item_name") : "").toString()); row1.createCell(3).setCellValue((map.get("fee_type_name") != null ? map.get("fee_type_name") : "").toString()); row1.createCell(4).setCellValue((map.get("calculate_subject") != null ? map.get("calculate_subject") : "").toString()); row1.createCell(5).setCellValue((map.get("detail_amount") != null ? map.get("detail_amount") : "").toString()); row1.createCell(6).setCellValue((map.get("source_order_no") != null ? map.get("source_order_no") : "").toString()); row1.createCell(7).setCellValue((map.get("customer_name") != null ? map.get("customer_name") : "").toString()); row1.createCell(8).setCellValue((map.get("source_type_name") != null ? map.get("source_type_name") : "").toString()); row1.createCell(9).setCellValue((map.get("sales_no") != null ? map.get("sales_no") : "").toString()); row1.createCell(10).setCellValue((map.get("receipt_box") != null ? map.get("receipt_box") : "").toString()); row1.createCell(11).setCellValue((map.get("deliver_no") != null ? map.get("deliver_no") : "").toString()); } //退貨配送費 HSSFSheet sheet5 = wb.createSheet("退貨配送費明細"); sheet5.setDefaultRowHeight((short) (2 * 256)); HSSFRow row5 = sheet5.createRow((int) 0); sheet5.createRow((int) 1); sheet5.createRow((int) 2); HSSFCell cell5 = row5.createCell(0); cell5.setCellValue("日期"); cell5.setCellStyle(style); cell5 = row5.createCell(1); cell5.setCellValue("公司名稱"); cell5.setCellStyle(style); cell5 = row5.createCell(2); cell5.setCellValue("商品名稱"); cell5.setCellStyle(style); cell5 = row5.createCell(3); cell5.setCellValue("費用區分"); cell5.setCellStyle(style); cell5 = row5.createCell(4); cell5.setCellValue("費項"); cell5.setCellStyle(style); cell5 = row5.createCell(5); cell5.setCellValue("金額"); cell5.setCellStyle(style); cell5 = row5.createCell(6); cell5.setCellValue("收貨單號"); cell5.setCellStyle(style); cell5 = row5.createCell(7); cell5.setCellValue("客戶"); cell5.setCellStyle(style); cell5 = row5.createCell(8); cell5.setCellValue("結算對象"); cell5.setCellStyle(style); cell5 = row5.createCell(9); cell5.setCellValue("銷售單號"); cell5.setCellStyle(style); cell5 = row5.createCell(10); cell5.setCellValue("退貨件數"); cell5.setCellStyle(style); cell5 = row5.createCell(11); cell5.setCellValue("總重量"); cell5.setCellStyle(style); cell5 = row5.createCell(12); cell5.setCellValue("送貨單號"); cell5.setCellStyle(style); List dealerReturnDeliverFee = SF.JYDEALER_SERVICE.dealerReturnDeliverFee(form); for (int i = 0; i < dealerReturnDeliverFee.size(); i++) { HSSFRow row1 = sheet5.createRow(i+1); Map<String, Object> map = (Map<String,Object>)dealerReturnDeliverFee.get(i); row1.createCell(0).setCellValue((map.get("confirm_date") != null ? map.get("confirm_date") : "").toString()); row1.createCell(1).setCellValue((map.get("name") != null ? map.get("name") : "").toString()); row1.createCell(2).setCellValue((map.get("packing_name") != null ? map.get("packing_name") : "").toString()); row1.createCell(3).setCellValue((map.get("fee_type_name") != null ? map.get("fee_type_name") : "").toString()); row1.createCell(4).setCellValue((map.get("calculate_subject") != null ? map.get("calculate_subject") : "").toString()); row1.createCell(5).setCellValue((map.get("detail_amount") != null ? map.get("detail_amount") : "").toString()); row1.createCell(6).setCellValue((map.get("source_order_no") != null ? map.get("source_order_no") : "").toString()); row1.createCell(7).setCellValue((map.get("customer_name") != null ? map.get("customer_name") : "").toString()); row1.createCell(8).setCellValue((map.get("source_type_name") != null ? map.get("source_type_name") : "").toString()); row1.createCell(9).setCellValue((map.get("sales_no") != null ? map.get("sales_no") : "").toString()); row1.createCell(10).setCellValue((map.get("receipt_box") != null ? map.get("receipt_box") : "").toString()); row1.createCell(11).setCellValue((map.get("detail_weight") != null ? map.get("detail_weight") : "").toString()); row1.createCell(12).setCellValue((map.get("deliver_no") != null ? map.get("deliver_no") : "").toString()); } //入出庫費 HSSFSheet sheet6 = wb.createSheet("入出庫費明細"); sheet6.setDefaultRowHeight((short) (2 * 256)); HSSFRow row6 = sheet6.createRow((int) 0); sheet6.createRow((int) 1); sheet6.createRow((int) 2); HSSFCell cell6 = row6.createCell(0); cell6.setCellValue("日期"); cell6.setCellStyle(style); cell6 = row6.createCell(1); cell6.setCellValue("公司名稱"); cell6.setCellStyle(style); cell6 = row6.createCell(2); cell6.setCellValue("費用區分"); cell6.setCellStyle(style); cell6 = row6.createCell(3); cell6.setCellValue("費項"); cell6.setCellStyle(style); cell6 = row6.createCell(4); cell6.setCellValue("結算對象"); cell6.setCellStyle(style); cell6 = row6.createCell(5); cell6.setCellValue("收貨單號"); cell6.setCellStyle(style); cell6 = row6.createCell(6); cell6.setCellValue("對應商品"); cell6.setCellStyle(style); cell6 = row6.createCell(7); cell6.setCellValue("件數"); cell6.setCellStyle(style); cell6 = row6.createCell(8); cell6.setCellValue("計算重量"); cell6.setCellStyle(style); cell6 = row6.createCell(9); cell6.setCellValue("費用"); cell6.setCellStyle(style); cell6 = row6.createCell(10); cell6.setCellValue("入庫類型"); cell6.setCellStyle(style); List dealerInoutFee = SF.JYDEALER_SERVICE.dealerInoutFee(form); for (int i = 0; i < dealerInoutFee.size(); i++) { HSSFRow row1 = sheet6.createRow(i+1); Map<String, Object> map = (Map<String,Object>)dealerInoutFee.get(i); row1.createCell(0).setCellValue((map.get("confirm_date") != null ? map.get("confirm_date") : "").toString()); row1.createCell(1).setCellValue((map.get("name") != null ? map.get("name") : "").toString()); row1.createCell(2).setCellValue((map.get("fee_type_name") != null ? map.get("fee_type_name") : "").toString()); row1.createCell(3).setCellValue((map.get("calculate_subject") != null ? map.get("calculate_subject") : "").toString()); row1.createCell(4).setCellValue((map.get("source_type_name") != null ? map.get("source_type_name") : "").toString()); row1.createCell(5).setCellValue((map.get("source_order_no") != null ? map.get("source_order_no") : "").toString()); row1.createCell(6).setCellValue((map.get("packing_name") != null ? map.get("packing_name") : "").toString()); row1.createCell(7).setCellValue((map.get("receipt_box") != null ? map.get("receipt_box") : "").toString()); row1.createCell(8).setCellValue((map.get("detail_weight") != null ? map.get("detail_weight") : "").toString()); row1.createCell(9).setCellValue((map.get("detail_amount") != null ? map.get("detail_amount") : "").toString()); row1.createCell(10).setCellValue((map.get("receipt_type") != null ? map.get("receipt_type") : "").toString()); } try { OutputStream out = response.getOutputStream(); wb.write(out); out.flush(); out.close(); } catch (ServiceException e) { logger.info("ServiceException=====導出excel異常====" + e); } catch (Exception e1) { logger.info("Exception=====導出excel異常====" + e1); } }
就是excel出來一行合併行 xxx年xx月xx日下面是內容
HSSFRow rowA = sheet.createRow((int) 0); sheet.createRow((int) 1); sheet.createRow((int) 2); sheet.createRow((int) 3); sheet.createRow((int) 4); sheet.createRow((int) 5); sheet.createRow((int) 6); sheet.createRow((int) 7); HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFCell cell = rowA.createCell(0); cell.setCellValue("庫存:"+form.getBeginDate()+"起至"+form.getEndDate()+""); cell.setCellStyle(style); cell = rowA.createCell(1); cell.setCellValue(""); cell.setCellStyle(style); cell = rowA.createCell(2); cell.setCellValue(""); cell.setCellStyle(style); cell = rowA.createCell(3); cell.setCellValue(""); cell.setCellStyle(style); cell = rowA.createCell(4); cell.setCellValue(""); cell.setCellStyle(style); cell = rowA.createCell(5); cell.setCellValue(""); cell.setCellStyle(style); cell = rowA.createCell(6); cell.setCellValue(""); cell.setCellStyle(style); cell = rowA.createCell(7); cell.setCellValue(""); cell.setCellStyle(style); // 合併單元格 CellRangeAddress cra =new CellRangeAddress(0, 0, 0, 10); // 起始行, 終止行, 起始列, 終止列 sheet.addMergedRegion(cra);
合併單元格後居中
style = wb.createCellStyle();
style.setAlignment(XSSFCellStyle.ALIGN_CENTER);