(后端)如何将数据库的表导出生成Excel?
1.如何通过元数据拿到数据库的信息?
2.如何用Java生成Excel表?
3.将数据库中的表导出生成Excel案例
如何通过元数据拿到数据库的信息
元数据:描述数据的数据
Java中使用元数据的两个方法
- DatabaseMetaData 通过连接可以拿到的信息:数据库软件,所有数据库名,所有数据库里面的表名,描述数据库的元数据
- ResultSetMetaData 拿到的表结构信息:获得表的列数目 类型和属性 ,描述数据库表的元数据
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")); } }
ResultSetMetaData的使用学习
@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表里面所有的内容~~~~
// 拿出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(); }
我把那到的表格信息输出
如何用Java生成Excel表?
需要一个插件工具包
@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")); }
将数据库中的表导出生成Excel案例
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) ( * ));
- HSSFFont font = wb.createFont();
- font.setFontName("宋体");
- font.setFontHeightInPoints((short) );
- HSSFRow row = sheet.createRow((int) );
- sheet.createRow((int) );
- sheet.createRow((int) );
- sheet.createRow((int) );
- sheet.createRow((int) );
- sheet.createRow((int) );
- sheet.createRow((int) );
- sheet.createRow((int) );
- HSSFCellStyle style = wb.createCellStyle();
- style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- HSSFCell cell = row.createCell();
- cell.setCellValue("收货");
- cell.setCellStyle(style);
- cell = row.createCell();
- cell.setCellValue("来源");
- cell.setCellStyle(style);
- cell = row.createCell();
- cell.setCellValue("入库");
- cell.setCellStyle(style);
- cell = row.createCell();
- cell.setCellValue("名称");
- cell.setCellStyle(style);
- cell = row.createCell();
- cell.setCellValue("商品名称");
- cell.setCellStyle(style);
- cell = row.createCell();
- cell.setCellValue("单位");
- cell.setCellStyle(style);
- cell = row.createCell();
- cell.setCellValue("规格");
- cell.setCellStyle(style);
- cell = row.createCell();
- cell.setCellValue("数量");
- cell.setCellStyle(style);
- cell = row.createCell();
- cell.setCellValue("单价");
- cell.setCellStyle(style);
- cell = row.createCell();
- cell.setCellValue("金额");
- cell.setCellStyle(style);
- cell = row.createCell();
- cell.setCellValue("用户名称");
- cell.setCellStyle(style);
- List list = SF.JYPURCHASEINFO_SERVICE.exportReceiptReport(form);
- for (int i = ; i < list.size(); i++) {
- HSSFRow row1 = sheet.createRow(i+);
- Map<String, Object> map = (Map<String,Object>)list.get(i);
- row1.createCell().setCellValue((map.get("receipt_no") != null ? map.get("receipt_no") : "").toString());
- row1.createCell().setCellValue((map.get("purchase_no") != null ? map.get("purchase_no") : "").toString());
- row1.createCell().setCellValue((map.get("value") != null ? map.get("value") : "").toString());
- row1.createCell().setCellValue((map.get("supplier_name") != null ? map.get("supplier_name") : "").toString());
- row1.createCell().setCellValue((map.get("packing_name") != null ? map.get("packing_name") : "").toString());
- row1.createCell().setCellValue((map.get("purchase_unit") != null ? map.get("purchase_unit") : "").toString());
- row1.createCell().setCellValue((map.get("packing_spec") != null ? map.get("packing_spec") : "").toString());
- row1.createCell().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().setCellValue((map.get("purchase_price") != null ? map.get("purchase_price") : "").toString());
- row1.createCell().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) ( * ));
- HSSFFont font = wb.createFont();
- font.setFontName("宋体");
- font.setFontHeightInPoints((short) );
- HSSFRow row = sheet.createRow((int) );
- sheet.createRow((int) );
- sheet.createRow((int) );
- sheet.createRow((int) );
- sheet.createRow((int) );
- sheet.createRow((int) );
- sheet.createRow((int) );
- sheet.createRow((int) );
- HSSFCellStyle style = wb.createCellStyle();
- style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- HSSFCell cell = row.createCell();
- cell.setCellValue("公司名称");
- cell.setCellStyle(style);
- cell = row.createCell();
- cell.setCellValue("仓储费");
- cell.setCellStyle(style);
- cell = row.createCell();
- cell.setCellValue("配送费");
- cell.setCellStyle(style);
- cell = row.createCell();
- cell.setCellValue("回空配送费");
- cell.setCellStyle(style);
- cell = row.createCell();
- cell.setCellValue("退货配偶费");
- cell.setCellStyle(style);
- cell = row.createCell();
- cell.setCellValue("入出库费");
- cell.setCellStyle(style);
- List dealerCosts = SF.JYDEALER_SERVICE.dealerCosts(form);
- for (int i = ; i < dealerCosts.size(); i++) {
- HSSFRow row1 = sheet.createRow(i+);
- Map<String, Object> map = (Map<String,Object>)dealerCosts.get(i);
- row1.createCell().setCellValue((map.get("name") != null ? map.get("name") : "").toString());
- row1.createCell().setCellValue((map.get("cc_fee") != null ? map.get("cc_fee") : "").toString());
- row1.createCell().setCellValue((map.get("ps_fee") != null ? map.get("ps_fee") : "").toString());
- row1.createCell().setCellValue((map.get("hkps_fee") != null ? map.get("hkps_fee") : "").toString());
- row1.createCell().setCellValue((map.get("thps_fee") != null ? map.get("thps_fee") : "").toString());
- row1.createCell().setCellValue((map.get("rc_fee") != null ? map.get("rc_fee") : "").toString());
- }
- //仓储费
- HSSFSheet sheet2 = wb.createSheet("仓储费明细");
- sheet2.setDefaultRowHeight((short) ( * ));
- HSSFRow row2 = sheet2.createRow((int) );
- sheet2.createRow((int) );
- sheet2.createRow((int) );
- sheet2.createRow((int) );
- sheet2.createRow((int) );
- sheet2.createRow((int) );
- sheet2.createRow((int) );
- sheet2.createRow((int) );
- HSSFCell cell2 = row2.createCell();
- cell2.setCellValue("日期");
- cell2.setCellStyle(style);
- cell2 = row2.createCell();
- cell2.setCellValue("公司名称");
- cell2.setCellStyle(style);
- cell2 = row2.createCell();
- cell2.setCellValue("商品类型");
- cell2.setCellStyle(style);
- cell2 = row2.createCell();
- cell2.setCellValue("商品名称");
- cell2.setCellStyle(style);
- cell2 = row2.createCell();
- cell2.setCellValue("库存件数");
- cell2.setCellStyle(style);
- cell2 = row2.createCell();
- cell2.setCellValue("总重量");
- cell2.setCellStyle(style);
- cell2 = row2.createCell();
- cell2.setCellValue("仓储费");
- cell2.setCellStyle(style);
- List dealerStorageFee = SF.JYDEALER_SERVICE.dealerStorageFee(form);
- for (int i = ; i < dealerStorageFee.size(); i++) {
- HSSFRow row1 = sheet2.createRow(i+);
- Map<String, Object> map = (Map<String,Object>)dealerStorageFee.get(i);
- row1.createCell().setCellValue((map.get("calendarDate") != null ? map.get("calendarDate") : "").toString());
- row1.createCell().setCellValue((map.get("dealerName") != null ? map.get("dealerName") : "").toString());
- row1.createCell().setCellValue((map.get("itemType") != null ? map.get("itemType") : "").toString());
- row1.createCell().setCellValue((map.get("packingName") != null ? map.get("packingName") : "").toString());
- row1.createCell().setCellValue((map.get("packingQty") != null ? map.get("packingQty") : "").toString());
- row1.createCell().setCellValue((map.get("totalWeight") != null ? map.get("totalWeight") : "").toString());
- row1.createCell().setCellValue((map.get("totalAmount") != null ? map.get("totalAmount") : "").toString());
- }
- //配送费
- HSSFSheet sheet3 = wb.createSheet("配送费明细");
- sheet3.setDefaultRowHeight((short) ( * ));
- HSSFRow row3 = sheet3.createRow((int) );
- sheet3.createRow((int) );
- sheet3.createRow((int) );
- sheet3.createRow((int) );
- sheet3.createRow((int) );
- sheet3.createRow((int) );
- sheet3.createRow((int) );
- sheet3.createRow((int) );
- HSSFCell cell3 = row3.createCell();
- cell3.setCellValue("日期");
- cell3.setCellStyle(style);
- cell3 = row3.createCell();
- cell3.setCellValue("公司名称");
- cell3.setCellStyle(style);
- cell3 = row3.createCell();
- cell3.setCellValue("费用区分");
- cell3.setCellStyle(style);
- cell3 = row3.createCell();
- cell3.setCellValue("费项");
- cell3.setCellStyle(style);
- cell3 = row3.createCell();
- cell3.setCellValue("金额");
- cell3.setCellStyle(style);
- cell3 = row3.createCell();
- cell3.setCellValue("客户");
- cell3.setCellStyle(style);
- cell3 = row3.createCell();
- cell3.setCellValue("结算对象");
- cell3.setCellStyle(style);
- cell3 = row3.createCell();
- cell3.setCellValue("订单编号");
- cell3.setCellStyle(style);
- cell3 = row3.createCell();
- cell3.setCellValue("送货件数");
- cell3.setCellStyle(style);
- cell3 = row3.createCell();
- cell3.setCellValue("总重量");
- cell3.setCellStyle(style);
- cell3 = row3.createCell();
- cell3.setCellValue("送货单号");
- cell3.setCellStyle(style);
- cell3 = row3.createCell();
- cell3.setCellValue("配送方式");
- cell3.setCellStyle(style);
- List dealerDeliverFee = SF.JYDEALER_SERVICE.dealerDeliverFee(form);
- for (int i = ; i < dealerDeliverFee.size(); i++) {
- HSSFRow row1 = sheet3.createRow(i+);
- Map<String, Object> map = (Map<String,Object>)dealerDeliverFee.get(i);
- row1.createCell().setCellValue((map.get("confirm_date") != null ? map.get("confirm_date") : "").toString());
- row1.createCell().setCellValue((map.get("name") != null ? map.get("name") : "").toString());
- row1.createCell().setCellValue((map.get("fee_type_name") != null ? map.get("fee_type_name") : "").toString());
- row1.createCell().setCellValue((map.get("calculate_subject") != null ? map.get("calculate_subject") : "").toString());
- row1.createCell().setCellValue((map.get("out_amount") != null ? map.get("out_amount") : "").toString());
- row1.createCell().setCellValue((map.get("customer_name") != null ? map.get("customer_name") : "").toString());
- row1.createCell().setCellValue((map.get("source_type_name") != null ? map.get("source_type_name") : "").toString());
- row1.createCell().setCellValue((map.get("source_order_no") != null ? map.get("source_order_no") : "").toString());
- row1.createCell().setCellValue((map.get("box_count") != null ? map.get("box_count") : "").toString());
- row1.createCell().setCellValue((map.get("weight") != null ? map.get("weight") : "").toString());
- row1.createCell().setCellValue((map.get("deliver_no") != null ? map.get("deliver_no") : "").toString());
- row1.createCell().setCellValue((map.get("value") != null ? map.get("value") : "").toString());
- }
- //回空配送费
- HSSFSheet sheet4 = wb.createSheet("回空配送费明细");
- sheet4.setDefaultRowHeight((short) ( * ));
- HSSFRow row4 = sheet4.createRow((int) );
- sheet4.createRow((int) );
- sheet4.createRow((int) );
- sheet4.createRow((int) );
- sheet4.createRow((int) );
- sheet4.createRow((int) );
- sheet4.createRow((int) );
- sheet4.createRow((int) );
- HSSFCell cell4 = row4.createCell();
- cell4.setCellValue("日期");
- cell4.setCellStyle(style);
- cell4 = row4.createCell();
- cell4.setCellValue("公司名称");
- cell4.setCellStyle(style);
- cell4 = row4.createCell();
- cell4.setCellValue("回空品名称");
- cell4.setCellStyle(style);
- cell4 = row4.createCell();
- cell4.setCellValue("费用区分");
- cell4.setCellStyle(style);
- cell4 = row4.createCell();
- cell4.setCellValue("费项");
- cell4.setCellStyle(style);
- cell4 = row4.createCell();
- cell4.setCellValue("金额");
- cell4.setCellStyle(style);
- cell4 = row4.createCell();
- cell4.setCellValue("收货单号");
- cell4.setCellStyle(style);
- cell4 = row4.createCell();
- cell4.setCellValue("客户");
- cell4.setCellStyle(style);
- cell4 = row4.createCell();
- cell4.setCellValue("结算对象");
- cell4.setCellStyle(style);
- cell4 = row4.createCell();
- cell4.setCellValue("销售单号");
- cell4.setCellStyle(style);
- cell4 = row4.createCell();
- cell4.setCellValue("回空件数");
- cell4.setCellStyle(style);
- cell4 = row4.createCell();
- cell4.setCellValue("送货单号");
- cell4.setCellStyle(style);
- List dealerBackDeliverFee = SF.JYDEALER_SERVICE.dealerBackDeliverFee(form);
- for (int i = ; i < dealerBackDeliverFee.size(); i++) {
- HSSFRow row1 = sheet4.createRow(i+);
- Map<String, Object> map = (Map<String,Object>)dealerBackDeliverFee.get(i);
- row1.createCell().setCellValue((map.get("confirm_date") != null ? map.get("confirm_date") : "").toString());
- row1.createCell().setCellValue((map.get("name") != null ? map.get("name") : "").toString());
- row1.createCell().setCellValue((map.get("item_name") != null ? map.get("item_name") : "").toString());
- row1.createCell().setCellValue((map.get("fee_type_name") != null ? map.get("fee_type_name") : "").toString());
- row1.createCell().setCellValue((map.get("calculate_subject") != null ? map.get("calculate_subject") : "").toString());
- row1.createCell().setCellValue((map.get("detail_amount") != null ? map.get("detail_amount") : "").toString());
- row1.createCell().setCellValue((map.get("source_order_no") != null ? map.get("source_order_no") : "").toString());
- row1.createCell().setCellValue((map.get("customer_name") != null ? map.get("customer_name") : "").toString());
- row1.createCell().setCellValue((map.get("source_type_name") != null ? map.get("source_type_name") : "").toString());
- row1.createCell().setCellValue((map.get("sales_no") != null ? map.get("sales_no") : "").toString());
- row1.createCell().setCellValue((map.get("receipt_box") != null ? map.get("receipt_box") : "").toString());
- row1.createCell().setCellValue((map.get("deliver_no") != null ? map.get("deliver_no") : "").toString());
- }
- //退货配送费
- HSSFSheet sheet5 = wb.createSheet("退货配送费明细");
- sheet5.setDefaultRowHeight((short) ( * ));
- HSSFRow row5 = sheet5.createRow((int) );
- sheet5.createRow((int) );
- sheet5.createRow((int) );
- HSSFCell cell5 = row5.createCell();
- cell5.setCellValue("日期");
- cell5.setCellStyle(style);
- cell5 = row5.createCell();
- cell5.setCellValue("公司名称");
- cell5.setCellStyle(style);
- cell5 = row5.createCell();
- cell5.setCellValue("商品名称");
- cell5.setCellStyle(style);
- cell5 = row5.createCell();
- cell5.setCellValue("费用区分");
- cell5.setCellStyle(style);
- cell5 = row5.createCell();
- cell5.setCellValue("费项");
- cell5.setCellStyle(style);
- cell5 = row5.createCell();
- cell5.setCellValue("金额");
- cell5.setCellStyle(style);
- cell5 = row5.createCell();
- cell5.setCellValue("收货单号");
- cell5.setCellStyle(style);
- cell5 = row5.createCell();
- cell5.setCellValue("客户");
- cell5.setCellStyle(style);
- cell5 = row5.createCell();
- cell5.setCellValue("结算对象");
- cell5.setCellStyle(style);
- cell5 = row5.createCell();
- cell5.setCellValue("销售单号");
- cell5.setCellStyle(style);
- cell5 = row5.createCell();
- cell5.setCellValue("退货件数");
- cell5.setCellStyle(style);
- cell5 = row5.createCell();
- cell5.setCellValue("总重量");
- cell5.setCellStyle(style);
- cell5 = row5.createCell();
- cell5.setCellValue("送货单号");
- cell5.setCellStyle(style);
- List dealerReturnDeliverFee = SF.JYDEALER_SERVICE.dealerReturnDeliverFee(form);
- for (int i = ; i < dealerReturnDeliverFee.size(); i++) {
- HSSFRow row1 = sheet5.createRow(i+);
- Map<String, Object> map = (Map<String,Object>)dealerReturnDeliverFee.get(i);
- row1.createCell().setCellValue((map.get("confirm_date") != null ? map.get("confirm_date") : "").toString());
- row1.createCell().setCellValue((map.get("name") != null ? map.get("name") : "").toString());
- row1.createCell().setCellValue((map.get("packing_name") != null ? map.get("packing_name") : "").toString());
- row1.createCell().setCellValue((map.get("fee_type_name") != null ? map.get("fee_type_name") : "").toString());
- row1.createCell().setCellValue((map.get("calculate_subject") != null ? map.get("calculate_subject") : "").toString());
- row1.createCell().setCellValue((map.get("detail_amount") != null ? map.get("detail_amount") : "").toString());
- row1.createCell().setCellValue((map.get("source_order_no") != null ? map.get("source_order_no") : "").toString());
- row1.createCell().setCellValue((map.get("customer_name") != null ? map.get("customer_name") : "").toString());
- row1.createCell().setCellValue((map.get("source_type_name") != null ? map.get("source_type_name") : "").toString());
- row1.createCell().setCellValue((map.get("sales_no") != null ? map.get("sales_no") : "").toString());
- row1.createCell().setCellValue((map.get("receipt_box") != null ? map.get("receipt_box") : "").toString());
- row1.createCell().setCellValue((map.get("detail_weight") != null ? map.get("detail_weight") : "").toString());
- row1.createCell().setCellValue((map.get("deliver_no") != null ? map.get("deliver_no") : "").toString());
- }
- //入出库费
- HSSFSheet sheet6 = wb.createSheet("入出库费明细");
- sheet6.setDefaultRowHeight((short) ( * ));
- HSSFRow row6 = sheet6.createRow((int) );
- sheet6.createRow((int) );
- sheet6.createRow((int) );
- HSSFCell cell6 = row6.createCell();
- cell6.setCellValue("日期");
- cell6.setCellStyle(style);
- cell6 = row6.createCell();
- cell6.setCellValue("公司名称");
- cell6.setCellStyle(style);
- cell6 = row6.createCell();
- cell6.setCellValue("费用区分");
- cell6.setCellStyle(style);
- cell6 = row6.createCell();
- cell6.setCellValue("费项");
- cell6.setCellStyle(style);
- cell6 = row6.createCell();
- cell6.setCellValue("结算对象");
- cell6.setCellStyle(style);
- cell6 = row6.createCell();
- cell6.setCellValue("收货单号");
- cell6.setCellStyle(style);
- cell6 = row6.createCell();
- cell6.setCellValue("对应商品");
- cell6.setCellStyle(style);
- cell6 = row6.createCell();
- cell6.setCellValue("件数");
- cell6.setCellStyle(style);
- cell6 = row6.createCell();
- cell6.setCellValue("计算重量");
- cell6.setCellStyle(style);
- cell6 = row6.createCell();
- cell6.setCellValue("费用");
- cell6.setCellStyle(style);
- cell6 = row6.createCell();
- cell6.setCellValue("入库类型");
- cell6.setCellStyle(style);
- List dealerInoutFee = SF.JYDEALER_SERVICE.dealerInoutFee(form);
- for (int i = ; i < dealerInoutFee.size(); i++) {
- HSSFRow row1 = sheet6.createRow(i+);
- Map<String, Object> map = (Map<String,Object>)dealerInoutFee.get(i);
- row1.createCell().setCellValue((map.get("confirm_date") != null ? map.get("confirm_date") : "").toString());
- row1.createCell().setCellValue((map.get("name") != null ? map.get("name") : "").toString());
- row1.createCell().setCellValue((map.get("fee_type_name") != null ? map.get("fee_type_name") : "").toString());
- row1.createCell().setCellValue((map.get("calculate_subject") != null ? map.get("calculate_subject") : "").toString());
- row1.createCell().setCellValue((map.get("source_type_name") != null ? map.get("source_type_name") : "").toString());
- row1.createCell().setCellValue((map.get("source_order_no") != null ? map.get("source_order_no") : "").toString());
- row1.createCell().setCellValue((map.get("packing_name") != null ? map.get("packing_name") : "").toString());
- row1.createCell().setCellValue((map.get("receipt_box") != null ? map.get("receipt_box") : "").toString());
- row1.createCell().setCellValue((map.get("detail_weight") != null ? map.get("detail_weight") : "").toString());
- row1.createCell().setCellValue((map.get("detail_amount") != null ? map.get("detail_amount") : "").toString());
- row1.createCell().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) );
- sheet.createRow((int) );
- sheet.createRow((int) );
- sheet.createRow((int) );
- sheet.createRow((int) );
- sheet.createRow((int) );
- sheet.createRow((int) );
- sheet.createRow((int) );
- HSSFCellStyle style = wb.createCellStyle();
- style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- HSSFCell cell = rowA.createCell();
- cell.setCellValue("库存:"+form.getBeginDate()+"起至"+form.getEndDate()+"");
- cell.setCellStyle(style);
- cell = rowA.createCell();
- cell.setCellValue("");
- cell.setCellStyle(style);
- cell = rowA.createCell();
- cell.setCellValue("");
- cell.setCellStyle(style);
- cell = rowA.createCell();
- cell.setCellValue("");
- cell.setCellStyle(style);
- cell = rowA.createCell();
- cell.setCellValue("");
- cell.setCellStyle(style);
- cell = rowA.createCell();
- cell.setCellValue("");
- cell.setCellStyle(style);
- cell = rowA.createCell();
- cell.setCellValue("");
- cell.setCellStyle(style);
- cell = rowA.createCell();
- cell.setCellValue("");
- cell.setCellStyle(style);
- // 合并单元格
- CellRangeAddress cra =new CellRangeAddress(, , , ); // 起始行, 终止行, 起始列, 终止列
- sheet.addMergedRegion(cra);
合并单元格后居中
- style = wb.createCellStyle();
- style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
(后端)如何将数据库的表导出生成Excel?的更多相关文章
- 从数据库的表导出到Excel表格中【让客户端下载的Excel】
原文发布时间为:2008-10-11 -- 来源于本人的百度文章 [由搬家工具导入] 这个例子是从gridview中导出到Excel,可以举一反三,可以直接从数据库中取值放在DataSet中,然后再从 ...
- 数据库多张表导出到excel
数据库多张表导出到excel public static void export() throws Exception{ //声明需要导出的数据库 String dbName = "hdcl ...
- 将Mysql的一张表导出至Excel格式文件
将Mysql的一张表导出至Excel格式文件 导出语句 进入mysql数据库,输入如下sql语句: select id, name, age from tablename into outfile ' ...
- Json数据导出生成Excel
最近在做一个导入导出Excel的功能,导出其他类型的文件都比较熟悉,但是导入跟导出一个Excel还是稍微特殊点.根据这次的经验,写了个导出的小样例. 总体思路就是json数据的key,value跟Ex ...
- Asp.Net Core 导入Excel数据到Sqlite数据库并重新导出到Excel
Asp.Net Core 导入Excel数据到Sqlite数据库并重新导出到Excel 在博文"在Asp.Net Core 使用 Sqlite 数据库"中创建了ASP.NET Co ...
- Python实战 :2017国考职业表excel转数据库,再查询生成excel
最近看2017年国考的职业表,多而杂,不好过滤我想要的信息,特此把它转成Sqlite3数据库,再从数据库里把查询结果导出成excel,方便找职业. (后附上整套代码) 环境:python2.7 x ...
- WPF根据Oracle数据库的表,生成CS文件小工具
开发小工具的原因: 1.我们公司的开发是客户端用C#,服务端用Java,前后台在通讯交互的时候,会用到Oracle数据库的字段,因为服务器端有公司总经理开发的一个根据Oracle数据库的表生成的cla ...
- JAVA利用JXL导出/生成 EXCEL
/** * 导出导出采暖市场部收入.成本.利润明细表 * @author JIA-G-Y */ public String exporExcel(String str) { String str=Se ...
- JAVA实现数据库数据导入/导出到Excel(POI)
准备工作: 1.导入POI包:POI下载地址http://mirrors.tuna.tsinghua.edu.cn/apache/poi/release/src/(重要) 如下 2.导入Java界面美 ...
随机推荐
- zuul熔断代码
package com.sun.fallback; import java.io.ByteArrayInputStream; import java.io.IOException; import ja ...
- Source Insight函数调用关系显示设置
当我们需要设置source Insight的项目代码中函数调用关系时,可通过如下的设置来实现: 1.显示函数调用关系窗口 Source Insight工具栏中“View”—>“Relation ...
- Tools - 一些代码阅读的方法
1 初始能力 让阅读思路清晰连贯,保持在程序的流程架构和逻辑实现上,不被语法.编程技巧和业务流程等频繁地阻碍和打断. 语言基础:熟悉基础语法,常用的函数.库.编程技巧等: 了解设计模式.构建工具.代码 ...
- win10 + cuda8.0 + caffe SSD + vs2015 + python3
一.下载 git clone https://github.com/runhang/caffe-ssd.git cd caffe-ssd 1. 修改 build_win.cmd if !PYTHON_ ...
- 服务器运维 -- windows系统更换System32下文件后 重启无法进入桌面
场景描述: windows系统更换System32下文件后 重启无法进入桌面 情况1,原替换文件有备份 解决建议: 准备好该文件 情况2,原备份文件没有备份 解决建议:从相同版本的服务器上边 ...
- redis内部分享ppt
作者:青客宝团队 Redis:最好的缓存数据库 说Redis是缓存服务,估计有些人会不开心,因为Redis也可以把数据库持久化,但是在大多数情况Redis的竞争力是提供缓存服务.说到缓存服务必然会想到 ...
- Unity 多人网络连接
NetWorkServer.cs using UnityEngine; using System.Collections; public class NetWorkServer : MonoBehav ...
- Python 虚拟环境 | Mac/Linux下如何避坑安装配置Virtualenv
1.为什么要使用虚拟环境 在Python中,不同的应用可能需要用到不同版本的第三方包,而这些第三方包被统一存放到目录site-packages中,不同版本的包容易相互覆盖,如安装Django 2.1时 ...
- 复刻smartbits的国产网络测试工具minismb简介
复刻smartbits的国产网络性能测试工具minismb,是一款专门用于测试智能路由器,网络交换机的性能和稳定性的软硬件相结合的工具.可以通过此工具测试任何ip网络设备的端口吞吐率,带 ...
- Eclipse Gradle 构建多模块项目
注意: 1.Eclipse不如IDEA智能,Eclipse建立的Gradle Project项目在目录级别上是同级的; 2.user-web模块如果要引用user-service模块,直接引用是找不到 ...