資料庫與Excel報表的動態產生,excel報表動態

來源:互聯網
上載者:User

資料庫與Excel報表的動態產生,excel報表動態
一.資料庫與Excel報表的動態產生

(1)讀取資料庫的資料動態產生Excel報表,這是JSP應用中常遇到的問題,本節採用的基本方法是:

在Excel工作薄中,將報表範本製作在第一張工作表中,從資料庫中讀取資料,利用POI組件複製模板工作表

而得到一張新的工作表,將查詢資料填寫到新的工作表中。 

(2) 執行個體分析

寫一個Servlet程式,查詢pubs資料庫的titles表和sales表,把查詢結果集資料填寫到圖6-14的報表中,

操作步驟如下:


第1步:建立一個名類為“DBExcelServlet”的Servlet類
第2步:編譯DBExcelServlet程式。


package my;import javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.util.*;import org.apache.poi.hssf.usermodel.*;import java.sql.*;import javax.sql.*;public class DBExcelServlet extends HttpServlet {     Connection con=null;  Statement st=null;  ResultSet rs=null;    protected void doGet(HttpServletRequest request,HttpServletResponse response)                           throws ServletException,java.io.IOException   {      ServletContext application=getServletContext() ;      ServletConfig config=getServletConfig() ;           response.setContentType("application/vnd.ms-excel"); //定義返回給用戶端的是excel資料檔案      ServletOutputStream out=response.getOutputStream();      HttpSession session =request.getSession();      request.setCharacterEncoding("gb2312");            String inputFileName="c:\\tomcat\\webapps\\ROOT\\bookSales.xls"; //存放報表檔案的路徑        HSSFWorkbook  sale  =      new  HSSFWorkbook(new  FileInputStream(inputFileName));   //開啟一個模板工作薄    HSSFSheet    sheet=null; //一個工作表的引用    HSSFRow  row=null;  //一個資料航的對象引用    HSSFCell  cell=null;//一個儲存格對象    int rowNumber=3;  //記錄當前資料行的行數    double sum=0;   //統計金額  try {   Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");   String url="jdbc:sqlserver://localhost:1433;databaseName=pubs;user=sa;password=";   con = DriverManager.getConnection(url);      String sql="select a.title_id,title,price,qty from titles a join sales b on a.title_id=b.title_id";   st=con.createStatement();   rs=st.executeQuery(sql);    sheet=sale.cloneSheet(0); //複製工作薄中的模板工作表 得到一張新的工作表      while(rs.next())   //遍曆迴圈   {    row=sheet.getRow(rowNumber); //獲得待填寫資料行對象            String title_id=rs.getString(1); cell=row.getCell((short)0); cell.setCellValue(title_id);  String title=rs.getString(2); cell=row.getCell((short)1); cell.setCellValue(title);  double price=rs.getDouble(3); cell=row.getCell((short)4); cell.setCellValue(price);  int qty=rs.getInt(4); cell=row.getCell((short)5); cell.setCellValue(qty);  double s=price*qty; cell=row.getCell((short)6); cell.setCellValue(s);  sum=sum+s;  rowNumber++; if(rowNumber==8) { row=sheet.getRow(8); cell=row.getCell((short)6); cell.setCellValue(sum); sum=0;  sheet=sale.cloneSheet(0); rowNumber=3; row=sheet.getRow(rowNumber); }   }      if(rowNumber<8)   {       row=sheet.getRow(8); cell=row.getCell((short)6); cell.setCellValue(sum);   }      rs.close();   st.close();   con.close();}catch(Exception e){  out.print(e.getMessage());  }   finally  {  try  {     if(rs!=null)   rs.close(); if(st!=null)   st.close(); if(con!=null)   con.close();  }catch(Exception em)  {System.out.println(em);}  }   sale.removeSheetAt(0);  //完成工作表填寫後,將工作薄中的模板工作表刪除,他是第一張工作表   sale.write(out); //將工作薄寫入response緩衝區,並返回給用戶端遊覽器顯示   out.close();   }        protected void doPost(HttpServletRequest request,HttpServletResponse response)                                 throws ServletException,java.io.IOException   {      doGet(request,response);   } }

(3)由於最近在很多的論壇看到自己的好多原創文章,希望大家在轉載的時候註明出處,是對我努力的更大肯定!謝謝支援!



Excel報表產生資料庫的問題

你把它們關聯起來不行嗎?
 
在Delphi中怎選擇欄位動態產生Excel報表

摘  要:利用Delphi設計介面,實現將資料庫系統中的資料,根據使用者選擇欄位,動態產生Excel報表,實現了列印部分資料的目的。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.