根據從前臺傳入的參數(param - X)、參數(tips_X_user,X爲參數個數)個數查出對應的sql語句,生成結果集(ResultSet),再根據結果集生成excel。html
控制類:ViewControllerjava
package org.tips.ctr; import java.sql.Connection; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.dbcp.BasicDataSource; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.multiaction.MultiActionController; import org.tips.dao.DBHelper; import org.tips.util.GetDBConfig; import org.tips.util.ViewExcel; /*** * 導出excel控制通用類 * @author tips *參數:param -X */ public class ViewController extends MultiActionController { /***set鏈接池 * <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" lazy-init="false"> */ private BasicDataSource dataSource; public BasicDataSource getDataSource() { return dataSource; } public void setDataSource(BasicDataSource dataSource) { this.dataSource = dataSource; } // 操做excel public ModelAndView viewExcel(HttpServletRequest request, HttpServletResponse response) throws Exception { List<String> params = new ArrayList<String>();// 保存條件參數 /*** * indexSQL的格式爲 tips_2_user,2表明2個條件參數 * 例如兩個參數從前臺傳過來,param0=scott && param1=tiger * 分別表明用戶名和密碼 */ String indexSQL = request.getParameter("indexSQL"); int i = 0; if (indexSQL != null && indexSQL.length() > 1) { StringBuffer sbf = new StringBuffer(indexSQL); Pattern pattern = Pattern.compile("_"); String[] str = pattern.split(sbf); String index = str[1]; i = Integer.parseInt(index); } //將頁面傳入的條件參數保存到ArrayList中 for (int j = 0; j < i; j++) { String param = request.getParameter("param" + j); params.add(param); } /*** * 從屬性文件中獲取sql語句(param0,param1) * indexSQL = select uname,pwd,sex,email from test_users where uname=? and pwd = ? */ String sql = GetDBConfig.getSQL(indexSQL); Connection conn = dataSource.getConnection(); DBHelper dbHelper = new DBHelper(conn);//數據增刪查改通用類 ResultSet rs = dbHelper.RsBySql(sql, params); //此處將conn傳入想在裏面關閉鏈接,在該類中關閉,則rs會等於null ViewExcel viewExcel = new ViewExcel(conn, rs); return new ModelAndView(viewExcel); } }
excel生成類:ViewExcel。
package org.tips.util; import java.sql.Connection; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.springframework.web.servlet.view.document.AbstractExcelView; public class ViewExcel extends AbstractExcelView { // 將數據集導出excel private Connection conn; private ResultSet rs; public ViewExcel() { } public ViewExcel(Connection conn, ResultSet rs) { this.conn = conn; this.rs = rs; } @Override protected void buildExcelDocument(Map arg0, HSSFWorkbook arg1, HttpServletRequest arg2, HttpServletResponse arg3) throws Exception { // TODO Auto-generated method stub HSSFSheet sheet = arg1.createSheet("list"); sheet.setDefaultColumnWidth(12);//表格寬度 if (rs == null) { HSSFCell cell = getCell(sheet, 0, 0); setText(cell, "No data"); } else { ResultSetMetaData rsmd = rs.getMetaData(); // 存儲所有的列名 String[] columnname = new String[rsmd.getColumnCount()]; for (int i = 0; i < columnname.length; i++) { columnname[i] = rsmd.getColumnName(i + 1); } //設置excel列名 for (int i = 0; i <= columnname.length; i++) { if (i != 0) { HSSFCell cell = getCell(sheet, 0, i); setText(cell, columnname[i-1]); } else { HSSFCell cell = getCell(sheet, 0, 0); setText(cell, "ROW"); } } // 導數據集 int row = 1; while (rs.next()) { HSSFRow sheetRow = sheet.createRow(row); sheetRow.createCell(0).setCellValue(row); for (int i = 1; i <= columnname.length; i++) { String tempValue = String.valueOf(rs.getObject(i)); sheetRow.createCell(i).setCellValue(tempValue); } row++; } } // 關閉數據集 if (rs != null) { rs.close(); } if (conn != null) { conn.rollback(); conn.close(); } } }
讀取SQL語句類:GetDBConfig
package org.tips.util; import java.io.IOException; import java.util.Properties; /*** * 獲取屬性文件中sql語句 * @author tips * */ public class GetDBConfig { private final static String CONFIG_PATH = "org\\tips_sql.properties"; public static String getSQL(String index){ String sql = ""; Properties proper = new Properties(); try { proper.load(GetDBConfig.class.getClassLoader().getResourceAsStream(CONFIG_PATH)); sql = proper.getProperty(index); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("找不到配置文件路徑!"); e.printStackTrace(); } return sql; } }
注意:在web.xml中需配置*.xls,這個請求在打開的保存提示中生成的文件擴展名是.xls,不然保存時需手動改爲.xls
<servlet> <servlet-name>tips</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>tips</servlet-name> <url-pattern>*.do</url-pattern> <url-pattern>*.xls</url-pattern> </servlet-mapping>
前臺body:
<a href="viewExcel.xls?indexSQL=tips_2_user&¶m0=scott&¶m1=tiger">tips_2_user</a>
tips_servlet.xml配置:
<!-- 操做excel --> <bean id="viewController" class="org.tips.ctr.ViewController"> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean>
在org\tips_sql.properties文件中添加:tips_2_user = select uname,pwd,sex,email from test_users where uname=? and pwd = ?
OK,這樣咱們就只要關注前臺傳遞的參數和tips_sql.properties文件中的sql就能夠了。web
--spring
--sql