1、最近項目基本進入最后階段了,然后會統計一下各個數據庫的各個數據表的數據量,開始使用的報表工具,report-designer,開源的,研究了兩天,發現並不是很好使,最后自己下班回去,晚上思考,想着還不如自己做一個,領導下命令,說這個活給你了,你做好給經理就行了。然后就開始不斷的做。思路大概如下所示:
第一步,鏈接各個數據源,由於項目的數據庫牽扯到mysql數據庫,postgresql數據庫,greenplum數據庫,然后mysql里面有十幾個庫,每個庫里面有相同的數據表,然后postgresql和greenplum是一個數據庫有相同的數據表。由於greenplum集群版性能很好,所以對於大數據量的話,用greenplum進行查詢十分方便快捷,也是關系型數據庫,和mysql的語法基本性一致。不扯這個了。
第二步,由於使用了maven項目的,所以引入依賴就行了。由於greenplum的jar包,在maven倉庫里面沒有找到,我就在maven項目的classpath里面引入了公司的包,如下所示:
在.classpath里面,最下面加入這一行,就引入我這個jar包。這個是公司/lib項目里面的jar包,greenplum的依賴回頭再找一下。
1 <classpathentry kind="lib" path="/lib/jdbc/greenplum.jar"/>
依賴如下所示:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.charts</groupId> 5 <artifactId>com.fline.aic.charts</artifactId> 6 <packaging>war</packaging> 7 <version>0.0.1-SNAPSHOT</version> 8 <name>com.fline.aic.charts Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 11 <dependencies> 12 <dependency> 13 <groupId>junit</groupId> 14 <artifactId>junit</artifactId> 15 <version>3.8.1</version> 16 <scope>test</scope> 17 </dependency> 18 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> 19 <dependency> 20 <groupId>org.apache.poi</groupId> 21 <artifactId>poi-ooxml</artifactId> 22 <version>3.9</version> 23 </dependency> 24 <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> 25 <dependency> 26 <groupId>mysql</groupId> 27 <artifactId>mysql-connector-java</artifactId> 28 <version>5.1.6</version> 29 </dependency> 30 <!-- https://mvnrepository.com/artifact/com.pivotal/greenplum-jdbc --> 31 <!-- <dependency> 32 <groupId>com.pivotal</groupId> 33 <artifactId>greenplum-jdbc</artifactId> 34 <version>5.1.4</version> 35 </dependency> --> 36 <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql --> 37 <dependency> 38 <groupId>org.postgresql</groupId> 39 <artifactId>postgresql</artifactId> 40 <version>42.1.4</version> 41 </dependency> 42 <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> 43 <dependency> 44 <groupId>com.alibaba</groupId> 45 <artifactId>fastjson</artifactId> 46 <version>1.2.47</version> 47 </dependency> 48 <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils --> 49 <dependency> 50 <groupId>commons-beanutils</groupId> 51 <artifactId>commons-beanutils</artifactId> 52 <version>1.9.3</version> 53 </dependency> 54 <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> 55 <dependency> 56 <groupId>org.apache.commons</groupId> 57 <artifactId>commons-lang3</artifactId> 58 <version>3.4</version> 59 </dependency> 60 <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --> 61 <dependency> 62 <groupId>commons-logging</groupId> 63 <artifactId>commons-logging</artifactId> 64 <version>1.1.1</version> 65 </dependency> 66 <!-- https://mvnrepository.com/artifact/commons-collections/commons-collections --> 67 <dependency> 68 <groupId>commons-collections</groupId> 69 <artifactId>commons-collections</artifactId> 70 <version>3.2.1</version> 71 </dependency> 72 <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> 73 <dependency> 74 <groupId>com.mchange</groupId> 75 <artifactId>c3p0</artifactId> 76 <version>0.9.5.2</version> 77 </dependency> 78 <!-- --> 79 <dependency> 80 <groupId>org.apache.poi</groupId> 81 <artifactId>poi-ooxml</artifactId> 82 <version>3.15</version> 83 </dependency> 84 <!-- https://mvnrepository.com/artifact/org.json/json --> 85 <dependency> 86 <groupId>org.json</groupId> 87 <artifactId>json</artifactId> 88 <version>20160810</version> 89 </dependency> 90 91 </dependencies> 92 <build> 93 <finalName>com.fline.aic.charts</finalName> 94 </build> 95 96 97 98 </project>
第三步、我使用了db.properties文件。放到src\main\resources路徑下面。然后由於牽扯到公司信息,這里面放了大概23個url連接。
形如如下所示:
1 #1.db_xxx 2 db_xxx_driver=com.mysql.jdbc.Driver 3 db_xxx_url=jdbc:mysql://xxx:3306/db_xxx 4 db_xxx_user=xxx5 db_xxx_password=xxx
......
第四步,搞一個連接的工具類。大概搞23個這樣的東西,重復代碼就行了,然后測試一下看看是否能夠連接成功。
1 package com.fline.aic.utils; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 import java.util.ResourceBundle; 9 10 /** 11 * 12 * @Description TODO 13 * @author biehl 14 * @Date 2018年9月21日 上午9:32:04 15 * 16 */ 17 public class JdbcUtils { 18 19 //1 20 private static String db_xxx_driver; 21 private static String db_xxx_url; 22 private static String db_xxx_user; 23 private static String db_xxx_password; 24 25 26 // 1 27 static { 28 db_xxx_driver = ResourceBundle.getBundle("db").getString("db_xxx_driver"); 29 db_xxx_url = ResourceBundle.getBundle("db").getString("db_xxx_url"); 30 db_xxx_user = ResourceBundle.getBundle("db").getString("db_xxx_user"); 31 db_xxx_password = ResourceBundle.getBundle("db").getString("db_xxx_password"); 32 } 33 34 /** 35 * 1 36 * @return 37 * @throws ClassNotFoundException 38 * @throws SQLException 39 */ 40 public static Connection getxxxConnection() throws ClassNotFoundException, SQLException { 41 // 加載數據庫驅動 42 Class.forName(db_xxx_driver); 43 // System.out.println("測試加載數據庫成功"); 44 Connection con = DriverManager.getConnection(db_xxx_url, db_xxx_user, db_xxx_password); 45 // System.out.println("測試數據庫鏈接成功"); 46 return con; 47 } 48 49 50 /** 51 * 52 * @param con 53 * @param ps 54 * @param rs 55 */ 56 public static void closeConnection(Connection con, PreparedStatement ps, ResultSet rs) { 57 if (rs != null) {// 關閉資源,避免出現異常 58 try { 59 rs.close(); 60 } catch (SQLException e) { 61 e.printStackTrace(); 62 } 63 } 64 if (ps != null) { 65 try { 66 ps.close(); 67 } catch (SQLException e) { 68 e.printStackTrace(); 69 } 70 } 71 if (con != null) { 72 try { 73 con.close(); 74 } catch (SQLException e) { 75 e.printStackTrace(); 76 } 77 } 78 } 79 80 public static void main(String[] args) { 81 try { 82 JdbcUtils.getxxxConnection(); 83 System.out.println("xxx前置庫連接成功....."); 84 System.out.println("======================================="); 85 86 } catch (ClassNotFoundException e) { 87 e.printStackTrace(); 88 } catch (SQLException e) { 89 e.printStackTrace(); 90 } 91 } 92 93 }
第五步、搞一個實體類,簡寫了這里。
1 package com.fline.aic.vo; 2 3 import java.io.Serializable; 4 5 /** 6 * 7 * @Description TODO 8 * @author biehl 9 * @Date 2018年9月21日 上午10:50:47 10 * 11 */ 12 public class CountEntity implements Serializable { 13 14 /** 15 * 16 */ 17 private static final long serialVersionUID = 1L; 18 private Integer sx;// xx 19 private Integer bj;// xx 20 private Integer yh;// xx 21 private Integer zz;// xx 22 23 public Integer getSx() { 24 return sx; 25 } 26 27 public void setSx(Integer sx) { 28 this.sx = sx; 29 } 30 31 public Integer getBj() { 32 return bj; 33 } 34 35 public void setBj(Integer bj) { 36 this.bj = bj; 37 } 38 39 public Integer getYh() { 40 return yh; 41 } 42 43 public void setYh(Integer yh) { 44 this.yh = yh; 45 } 46 47 public Integer getZz() { 48 return zz; 49 } 50 51 public void setZz(Integer zz) { 52 this.zz = zz; 53 } 54 55 public CountEntity(Integer sx, Integer bj, Integer yh, Integer zz) { 56 super(); 57 this.sx = sx; 58 this.bj = bj; 59 this.yh = yh; 60 this.zz = zz; 61 } 62 63 public CountEntity() { 64 super(); 65 } 66 67 @Override 68 public String toString() { 69 return "CountEntity [sx=" + sx + ", bj=" + bj + ", yh=" + yh + ", zz=" + zz + "]"; 70 } 71 72 }
第六步、查詢一下,統計報表數據量。
package com.fline.aic.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.fline.aic.utils.JdbcUtils; import com.fline.aic.vo.CountEntity; /** * * @Description TODO * @author biehl * @Date 2018年9月21日 上午10:33:03 * */ public class QueryDataOfCharts { private static QueryDataOfCharts queryDataOfCharts; private QueryDataOfCharts() { } public static QueryDataOfCharts getInstance() { if (queryDataOfCharts == null) { queryDataOfCharts = new QueryDataOfCharts(); } return queryDataOfCharts; } public Connection con = null; public PreparedStatement ps = null; public ResultSet rs = null; /** * 1 * * @return */ public CountEntity queryDbxxx() { try { Connection xxxConnection = JdbcUtils.getxxxConnection(); String sql = "select\r\n" + " (sx_directory.sx + sx_general_basic.sx + sx_general_extend.sx + sx_general_material.sx + sx_general_fee_project.sx + sx_general_questions.sx + sx_punish_basic.sx + sx_punish_questions.sx + sx_handle_basic.sx + sx_handle_material.sx + sx_handle_questions.sx + sx_public_basic.sx + sx_public_extend.sx + sx_public_material.sx + sx_public_fee_project.sx + sx_public_questions.sx + sx_check_basic.sx + sx_check_questions.sx + sx_zone_organization.sx) as sx,(bj_pro_accept.bj + bj_pro_process.bj + bj_pro_result.bj + bj_pro_specialprocedure.bj + bj_pro_material.bj) as bj,(yh_uc_province_user.yh + yh_uc_corporator_identity.yh + yh_uc_corporator_account.yh + yh_uc_info_enterprise.yh + yh_uc_info_association.yh + yh_uc_info_central_dept.yh + yh_uc_gov_org.yh + yh_uc_gov_region.yh + yh_uc_gov_staff.yh) as yh,(zz_lic_data.zz) as zz\r\n" + "from \r\n" + "(select count(1) as sx from up_task_directory) as sx_directory JOIN\r\n" + "(select count(1) as sx from up_task_general_basic) as sx_general_basic ON 1=1 JOIN\r\n" + "(select count(1) as sx from up_task_general_extend) as sx_general_extend ON 1=1 JOIN\r\n" + "(select count(1) as sx from up_task_general_material) as sx_general_material ON 1=1 JOIN\r\n" + "(select count(1) as sx from up_task_general_fee_project) as sx_general_fee_project ON 1=1 JOIN\r\n" + "(select count(1) as sx from up_task_general_questions) as sx_general_questions ON 1=1 JOIN\r\n" + "(select count(1) as sx from up_task_punish_basic) as sx_punish_basic ON 1=1 JOIN\r\n" + "(select count(1) as sx from up_task_punish_questions) as sx_punish_questions ON 1=1 JOIN\r\n" + "(select count(1) as sx from up_task_handle_basic) as sx_handle_basic ON 1=1 JOIN\r\n" + "(select count(1) as sx from up_task_handle_material) as sx_handle_material ON 1=1 JOIN\r\n" + "(select count(1) as sx from up_task_handle_questions) as sx_handle_questions ON 1=1 JOIN\r\n" + "(select count(1) as sx from up_task_public_basic) as sx_public_basic ON 1=1 JOIN\r\n" + "(select count(1) as sx from up_task_public_extend) as sx_public_extend ON 1=1 JOIN\r\n" + "(select count(1) as sx from up_task_public_material) as sx_public_material ON 1=1 JOIN\r\n" + "(select count(1) as sx from up_task_public_fee_project) as sx_public_fee_project ON 1=1 JOIN\r\n" + "(select count(1) as sx from up_task_public_questions) as sx_public_questions ON 1=1 JOIN\r\n" + "(select count(1) as sx from up_task_check_basic) as sx_check_basic ON 1=1 JOIN\r\n" + "(select count(1) as sx from up_task_check_questions) as sx_check_questions ON 1=1 JOIN\r\n" + "(select count(1) as sx from up_zone_organization) as sx_zone_organization ON 1=1 JOIN\r\n" + "(select count(1) as bj from up_pro_accept) as bj_pro_accept ON 1=1 JOIN\r\n" + "(select count(1) as bj from up_pro_process) as bj_pro_process ON 1=1 JOIN\r\n" + "(select count(1) as bj from up_pro_result) as bj_pro_result ON 1=1 JOIN\r\n" + "(select count(1) as bj from up_pro_specialprocedure) as bj_pro_specialprocedure ON 1=1 JOIN\r\n" + "(select count(1) as bj from up_pro_material) as bj_pro_material ON 1=1 JOIN \r\n" + "(select count(1) as yh from up_uc_province_user) as yh_uc_province_user ON 1=1 JOIN\r\n" + "(select count(1) as yh from up_uc_corporator_identity) as yh_uc_corporator_identity ON 1=1 JOIN\r\n" + "(select count(1) as yh from up_uc_corporator_account) as yh_uc_corporator_account ON 1=1 JOIN\r\n" + "(select count(1) as yh from up_uc_info_enterprise) as yh_uc_info_enterprise ON 1=1 JOIN\r\n" + "(select count(1) as yh from up_uc_info_association) as yh_uc_info_association ON 1=1 JOIN \r\n" + "(select count(1) as yh from up_uc_info_central_dept) as yh_uc_info_central_dept ON 1=1 JOIN\r\n" + "(select count(1) as yh from up_uc_gov_org) as yh_uc_gov_org ON 1=1 JOIN\r\n" + "(select count(1) as yh from up_uc_gov_region) as yh_uc_gov_region ON 1=1 JOIN\r\n" + "(select count(1) as yh from up_uc_gov_staff) as yh_uc_gov_staff ON 1=1 JOIN\r\n" + "(select count(1) as zz from up_lic_data) as zz_lic_data ON 1=1"; ps = xxxConnection.prepareStatement(sql); rs = ps.executeQuery(); CountEntity ce = null; if (rs.next()) { ce = new CountEntity(); ce.setSx(rs.getInt("sx")); ce.setBj(rs.getInt("bj")); ce.setYh(rs.getInt("yh")); ce.setZz(rs.getInt("zz")); return ce; } else { return null; } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return null; } public static void main(String[] args) { QueryDataOfCharts instance = QueryDataOfCharts.getInstance(); CountEntity queryDbxxx = instance.queryDbxxx(); System.out.println( "xxx " + queryDbxxx + "\n" + "========================================================="); } }
其實巴拉巴拉一大堆,我感覺上面這個大sql才是比較有意思的東西。其實好好看看挺好的,就是把一類的統計相加,然后最后輸出到excel里面,還是比較有意思的。
第七步,就是將查詢的數據量輸出到excel里面就行了:
統計報表就有意思了,將統計的數據量放到list里面,然后將list放到map里面。這樣一行的都放到list里面。不同行放到不同的list里面,這樣循環遍歷輸出的時候就可以將不同的放到不同的行里面,完美的解決我的報表統計功能。
package com.fline.aic.excel; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import com.fline.aic.dao.QueryDataOfCharts; import com.fline.aic.vo.CountEntity; /** * * @Description TODO * @author biehl * @Date 2018年9月21日 上午11:37:28 * */ public class WriteExcelForXSSF { private static WriteExcelForXSSF writeExcelForXSSF; private WriteExcelForXSSF() { } public static WriteExcelForXSSF getInstance() { if (writeExcelForXSSF == null) { writeExcelForXSSF = new WriteExcelForXSSF(); } return writeExcelForXSSF; } /** * */ public Map<Integer, List<Integer>> readDbAreaOfDabase() { // Map集合 Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>(); // List集合 List<Integer> list = new ArrayList<Integer>(); // 獲取到QueryDataOfCharts對象 QueryDataOfCharts instance = QueryDataOfCharts.getInstance(); // 查詢到xxx的數據 CountEntity queryDbxxx = instance.queryDbxxx(); list.add(queryDbxxx.getSx()); list.add(queryDbxxx.getBj()); list.add(queryDbxxx.getZz()); list.add(queryDbxxx.getYh()); map.put(0,list); return map; } public void writeDbAreaForXSSF() { // 創建一個空的工作簿 Workbook workbook = new XSSFWorkbook(); // 創建一個sheet頁 Sheet sheet = workbook.createSheet("xxxxxx報表"); // 合並單元格 /* * sheet.addMergedRegion(new CellRangeAddress( 2,//第一行(從0開始) 2,//最后一行(從0開始) * 0,//第一列(從0開始) 26 //最后一列(從0開始) )); */ // 創建一行,開始是0行,設置第2行 Row row = sheet.createRow(1); // 創建一個單元格,第一列 // Cell cell = row.createCell(1); // 第一行第一列設置值 // cell.setCellValue("資源共享服務中心數據匯聚統計表"); // row.createCell(0).setCellValue("資源共享服務中心數據匯聚統計表"); // 設置字體 Font font = workbook.createFont(); font.setFontHeightInPoints((short) 20); font.setFontName("Courier New"); font.setBold(true); // 設置數字的字體 Font font2 = workbook.createFont(); font2.setFontHeightInPoints((short) 10); font2.setFontName("Courier New"); font2.setBold(true); // 設置樣式 CellStyle cs = workbook.createCellStyle(); cs.setFont(font); CellStyle cs2 = workbook.createCellStyle(); cs2.setFont(font2); // 將要設置字體的單元格進行設置 // 創建一個單元格,第一列 Cell cell = row.createCell(1); // 第一行第一列設置值 cell.setCellValue("資源共享服務中心數據匯聚統計表"); cell.setCellStyle(cs); // 設置一行 Row row3 = sheet.createRow(3); // 創建一列,第一列設置地方前置庫名稱 Cell cell3 = row3.createCell(0); // 為這一行這一列設置值 cell3.setCellValue("xxx"); cell3.setCellStyle(cs2); // 設置一行 Row row4 = sheet.createRow(4); // 創建一列,第一列設置地方前置庫名稱 Cell cell4 = row4.createCell(0); // 為這一行這一列設置值 cell4.setCellValue("廣東"); cell4.setCellStyle(cs2); // 設置一行 Row row5 = sheet.createRow(5); // 創建一列,第一列設置地方前置庫名稱 Cell cell5 = row5.createCell(0); // 為這一行這一列設置值 cell5.setCellValue("江蘇"); cell5.setCellStyle(cs2); // 設置一行 Row row6 = sheet.createRow(6); // 創建一列,第一列設置地方前置庫名稱 Cell cell6 = row6.createCell(0); // 為這一行這一列設置值 cell6.setCellValue("貴州"); cell6.setCellStyle(cs2); // 設置一行 Row row7 = sheet.createRow(7); // 創建一列,第一列設置地方前置庫名稱 Cell cell7 = row7.createCell(0); // 為這一行這一列設置值 cell7.setCellValue("山東"); cell7.setCellStyle(cs2); // 設置一行 Row row8 = sheet.createRow(8); // 創建一列,第一列設置地方前置庫名稱 Cell cell8 = row8.createCell(0); // 為這一行這一列設置值 cell8.setCellValue("上海"); cell8.setCellStyle(cs2); // 設置一行 Row row9 = sheet.createRow(9); // 創建一列,第一列設置地方前置庫名稱 Cell cell9 = row9.createCell(0); // 為這一行這一列設置值 cell9.setCellValue("安徽"); cell9.setCellStyle(cs2); // 設置一行 Row row10 = sheet.createRow(10); // 創建一列,第一列設置地方前置庫名稱 Cell cell10 = row10.createCell(0); // 為這一行這一列設置值 cell10.setCellValue("四川"); cell10.setCellStyle(cs2); // 設置一行 Row row11 = sheet.createRow(11); // 創建一列,第一列設置地方前置庫名稱 Cell cell11 = row11.createCell(0); // 為這一行這一列設置值 cell11.setCellValue("重慶"); cell11.setCellStyle(cs2); Row row12 = sheet.createRow(2); // 創建一列,第一列設置地方前置庫名稱 Cell cell12 = row12.createCell(1); // 為這一行這一列設置值 cell12.setCellValue("脫敏后中間庫"); cell12.setCellStyle(cs2); // 創建一列,第一列設置地方前置庫名稱 Cell cell13 = row12.createCell(5); // 為這一行這一列設置值 cell13.setCellValue("匯聚數據區"); cell13.setCellStyle(cs2); // 創建一列,第一列設置地方前置庫名稱 Cell cell14 = row12.createCell(9); // 為這一行這一列設置值 cell14.setCellValue("匯聚前置庫"); cell14.setCellStyle(cs2); // 創建一列,第一列設置地方前置庫名稱 Cell cell15 = row12.createCell(12); // 為這一行這一列設置值 cell15.setCellValue("應用前置庫"); cell15.setCellStyle(cs2); // 創建一列,第一列設置地方前置庫名稱 Cell cell16 = row12.createCell(15); // 為這一行這一列設置值 cell16.setCellValue("核心數據區"); cell16.setCellStyle(cs2); // 創建一列,第一列設置地方前置庫名稱 Cell cell17 = row12.createCell(19); // 為這一行這一列設置值 cell17.setCellValue("共享前置庫"); cell17.setCellStyle(cs2); //xxx,xxx,xxx,xxx Row row13 = sheet.createRow(3); // 創建一列,第一列設置地方前置庫名稱 Cell cell18 = row13.createCell(1); // 為這一行這一列設置值 cell18.setCellValue("xxx"); cell18.setCellStyle(cs2); Cell cell19 = row13.createCell(2); // 為這一行這一列設置值 cell19.setCellValue("xxx"); cell19.setCellStyle(cs2); Cell cell20 = row13.createCell(3); // 為這一行這一列設置值 cell20.setCellValue("xxx"); cell20.setCellStyle(cs2); Cell cell21 = row13.createCell(4); // 為這一行這一列設置值 cell21.setCellValue("xxx"); cell21.setCellStyle(cs2); Cell cell22 = row13.createCell(5); // 為這一行這一列設置值 cell22.setCellValue("xxx"); cell22.setCellStyle(cs2); Cell cell23 = row13.createCell(6); // 為這一行這一列設置值 cell23.setCellValue("xxx"); cell23.setCellStyle(cs2); Cell cell24 = row13.createCell(7); // 為這一行這一列設置值 cell24.setCellValue("xxx"); cell24.setCellStyle(cs2); Cell cell25 = row13.createCell(8); // 為這一行這一列設置值 cell25.setCellValue("xxx"); cell25.setCellStyle(cs2); Cell cell26 = row13.createCell(9); // 為這一行這一列設置值 cell26.setCellValue("xxx"); cell26.setCellStyle(cs2); Cell cell27 = row13.createCell(10); // 為這一行這一列設置值 cell27.setCellValue("xxx"); cell27.setCellStyle(cs2); Cell cell28 = row13.createCell(11); // 為這一行這一列設置值 cell28.setCellValue("xxx"); cell28.setCellStyle(cs2); Cell cell29 = row13.createCell(12); // 為這一行這一列設置值 cell29.setCellValue("xxx"); cell29.setCellStyle(cs2); Cell cell30 = row13.createCell(13); // 為這一行這一列設置值 cell30.setCellValue("xxx"); cell30.setCellStyle(cs2); Cell cell31 = row13.createCell(14); // 為這一行這一列設置值 cell31.setCellValue("xxx"); cell31.setCellStyle(cs2); Cell cell32 = row13.createCell(15); // 為這一行這一列設置值 cell32.setCellValue("xxx"); cell32.setCellStyle(cs2); Cell cell33 = row13.createCell(16); // 為這一行這一列設置值 cell33.setCellValue("xxx"); cell33.setCellStyle(cs2); Cell cell34 = row13.createCell(17); // 為這一行這一列設置值 cell34.setCellValue("xxx"); cell34.setCellStyle(cs2); Cell cell35 = row13.createCell(18); // 為這一行這一列設置值 cell35.setCellValue("xxx"); cell35.setCellStyle(cs2); Cell cell36 = row13.createCell(19); // 為這一行這一列設置值 cell36.setCellValue("xxx"); cell36.setCellStyle(cs2); Cell cell37 = row13.createCell(20); // 為這一行這一列設置值 cell37.setCellValue("xxx"); cell37.setCellStyle(cs2); int sxCount = 0;// xxx int bjCount = 0;// xxx int yhCount = 0;// xxx int zzCount = 0;// xxx int sumCount = 0;// xxx,xxx,xxx,xxx總計 // 讀取查詢的xxx數據庫的統計數據 WriteExcelForXSSF instance = WriteExcelForXSSF.getInstance(); Map<Integer, List<Integer>> readDbAreaOfDabase = instance.readDbAreaOfDabase(); for (int i = 0; i < readDbAreaOfDabase.size(); i++) { List<Integer> list = readDbAreaOfDabase.get(i); // 設置一行 Row row2 = sheet.createRow(i + 4); for (int j = 0; j < list.size(); j++) { // 創建一列,第二列設置數值 Cell cell2 = row2.createCell(j + 1); // 獲取這一行一這列的值 Integer value = list.get(j); // 為這一行這一列設置值 cell2.setCellValue(value); cell2.setCellStyle(cs2); //打印輸出合計數量 //System.out.println(sxCount + "," + bjCount + "," + yhCount + "," + zzCount); } } // 創建輸出流 try { File file = new File("C:\\Users\\Aiyufei\\Desktop\\poi.xlsx"); if (file.exists()) { file.delete(); } else { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } FileOutputStream fos = new FileOutputStream(file); System.out.println(file.getName() + " ,excel文件已經成功創建....."); try { // 寫入流中,創建此excel workbook.write(fos); } catch (IOException e) { e.printStackTrace(); } try { // 關閉流 fos.close(); } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); System.out.println("開始時間:" + sdf.format(new Date())); WriteExcelForXSSF instance = WriteExcelForXSSF.getInstance(); instance.writeDbAreaForXSSF(); System.out.println("結束時間:" + sdf.format(new Date())); } }
待續.....