首先新建一個maven項目,添加相關依賴:
使用poi導出excel依賴:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.16</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.16</version> </dependency>
添加csv相關工具類依賴:
<!-- https://mvnrepository.com/artifact/net.sourceforge.javacsv/javacsv --> <dependency> <groupId>net.sourceforge.javacsv</groupId> <artifactId>javacsv</artifactId> <version>2.0</version> </dependency>
2、構建執行代碼
(1)、數據導出為excel文件:
package com.hwinfo.excel.poi.export; import com.hwinfo.excel.poi.util.OutputUtil; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.util.CellRangeAddress; import java.io.FileOutputStream; import java.io.IOException; import java.sql.*; /** * @Time : 2019/4/8 0008 14:05 * @Author : lisheng * @Description: **/ public class ExportExcel { // private static final String URL = "jdbc:mysql://192.168.101.217:3306/test"; // private static final String NAME = "dev"; // private static final String PASSWORD = "lJZx2Ik5eqX3xBDp"; private static final String URL = "jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8"; private static final String NAME = "root"; private static final String PASSWORD = "123456"; public static void main(String[] args) throws Exception { //1.加載驅動程序 Class.forName("com.mysql.jdbc.Driver"); //2.獲得數據庫的連接 Connection conn = DriverManager.getConnection(URL, NAME, PASSWORD); //3.通過數據庫的連接操作數據庫,實現增刪改查 Statement stmt = conn.createStatement(); Statement statement = conn.createStatement(); // TODO 獲取數據 ResultSet resultSet = statement.executeQuery(" select * from area_code order by code limit 50000"); ResultSetMetaData metaData = resultSet.getMetaData(); int columnCount = metaData.getColumnCount(); // TODO 創建HSSFWorkbook對象(excel的文檔對象) HSSFWorkbook wb = new HSSFWorkbook(); // TODO 設置字體格式大小 HSSFFont font = wb.createFont(); font.setFontName("宋體"); font.setFontHeightInPoints((short) 14);//設置字體大小 HSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setAlignment(HorizontalAlignment.CENTER); // 居中 cellStyle.setFont(font); //建立新的sheet對象(excel的表單) HSSFSheet sheet = wb.createSheet("用戶表"); //在sheet里創建第一行,參數為行索引(excel的行),可以是0~65535之間的任何一個 HSSFRow row1 = sheet.createRow(0); row1.setHeight((short)500); //創建單元格(excel的單元格,參數為列索引,可以是0~255之間的任何一個 HSSFCell cell = row1.createCell(0); //設置單元格內容 cell.setCellStyle(cellStyle); cell.setCellValue("用戶信息表"); //合並單元格CellRangeAddress構造參數依次表示起始行,截至行,起始列, 截至列 sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, columnCount-1)); //在sheet里創建第二行 HSSFRow row2 = sheet.createRow(1); row2.setHeight((short) 500); //創建單元格並設置單元格內容 for (int i = 0; i < columnCount; i++) { sheet.setColumnWidth(i,30*256); HSSFCell cell1 = row2.createCell(i); cell1.setCellStyle(cellStyle); cell1.setCellValue(metaData.getColumnName(i + 1)); } int b = 2; //數據導入單元格 while (resultSet.next()) { HSSFRow row = sheet.createRow(b); row.setHeight((short) 500); for (int i = 0; i < columnCount; i++) { HSSFCell cell1 = row.createCell(i); cell1.setCellStyle(cellStyle); cell1.setCellValue(resultSet.getString(metaData.getColumnName(i + 1))); } b++; } //輸出Excel文件 try { FileOutputStream output = new FileOutputStream("d:\\detail.xls"); wb.write(output); output.close(); } catch (Exception e) { e.printStackTrace(); } statement.close(); conn.close(); System.out.println("文件成功導出"); } }
(2)、導出為csv
package com.hwinfo.excel.poi.export; import com.csvreader.CsvWriter; import java.io.*; import java.nio.charset.Charset; import java.sql.*; /** * @Time : 2019/6/5 0005 15:27 * @Author : lisheng * @Description: **/ public class ExportCsv { // private static final String URL = "jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8"; // private static final String NAME = "root"; // private static final String PASSWORD = "123456"; private static final String URL = "jdbc:postgresql://host:port/db"; private static final String NAME = "user"; private static final String PASSWORD = "pwd"; public static void main(String[] args) throws Exception { //1.加載驅動程序 // Class.forName("com.mysql.jdbc.Driver"); Class.forName("org.postgresql.Driver"); //2.獲得數據庫的連接 Connection conn = DriverManager.getConnection(URL, NAME, PASSWORD); //3.通過數據庫的連接操作數據庫,實現增刪改查 Statement stmt = conn.createStatement(); Statement statement = conn.createStatement(); // TODO 獲取數據 // ResultSet resultSet = statement.executeQuery(" select * from area_code order by code limit 50000"); ResultSet resultSet = statement.executeQuery("select * from area_code;"); ResultSetMetaData metaData = resultSet.getMetaData(); int columnCount = metaData.getColumnCount(); FileOutputStream out = new FileOutputStream("d:\\detail.csv"); try { System.out.println("d:\\detail.csv"); CsvWriter csvWriter = new CsvWriter("d:\\detail.csv", ',', Charset.forName("UTF-8")); //寫入表頭信息 String[] header = new String[columnCount]; for (int i = 0; i < columnCount; i++) { header[i] = metaData.getColumnName(i + 1); } csvWriter.writeRecord(header); //寫入內容信息 while (resultSet.next()) { for (int i = 0; i < columnCount; i++) { csvWriter.write(resultSet.getString(metaData.getColumnName(i + 1))); } csvWriter.endRecord(); } //關閉寫入的流 csvWriter.close(); File fileLoad = new File("d:\\detail.csv"); FileInputStream in = new java.io.FileInputStream(fileLoad); //每次寫入10240個字節 byte[] b = new byte[10240]; int n; while ((n = in.read(b)) != -1) { out.write(b, 0, n); //每次寫入out1024字節 } out.close(); in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }