Java如何將資料從資料庫插入到電子試算表中?

2019-10-16 22:27:52

在Java程式設計中,如何將資料從資料庫插入到電子試算表中?

注意:需要存取網址:http://poi.apache.org/download.html , 下載一個Apache POI軟體包。這裡下載最新版本:poi-bin-3.17-20170915.tar.gz解壓並將全部.jar檔案匯入 。

需要匯入全部包(在原來的基礎上增加連線到資料庫的jar包),如下圖所示 -

參考範例:

http://poi.apache.org/spreadsheet/quick-guide.html

在資料庫:testdb中,建立一個emp表,其表結構和資料記錄如下 -

CREATE TABLE `emp` (
  `emp_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(64) NOT NULL DEFAULT '',
  `age` tinyint(2) unsigned NOT NULL DEFAULT '0',
  `salary` float(10,2) unsigned DEFAULT '0.00',
  `dept` varchar(16) DEFAULT NULL,
  PRIMARY KEY (`emp_id`)
) ENGINE=InnoDB AUTO_INCREMENT=103 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of emp
-- ----------------------------
INSERT INTO `emp` VALUES ('100', '王大大', '25', '8100.00', '技術');
INSERT INTO `emp` VALUES ('101', ' 李小偉', '38', '1200.00', '市場');
INSERT INTO `emp` VALUES ('102', ' 張方擇', '29', '13500.00', '綜合');

以下是使用Java將資料從資料庫插入到電子試算表中的程式。

package com.yiibai;

import java.io.File;
import java.io.FileOutputStream;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class InsertDataFromDataBaseToSpreadSheet {
   public static void main(String[] args) throws Exception {

      //Connecting to the database
      Class.forName("com.mysql.jdbc.Driver");
      Connection connect = DriverManager.getConnection(
         "jdbc:mysql://localhost:3306/testdb?useSSL=false", "root" , "123456");

      //Getting data from the table emp_tbl
      Statement statement = connect.createStatement();
      ResultSet resultSet = statement.executeQuery("SELECT * FROM emp");

      //Creating a Work Book
      XSSFWorkbook workbook = new XSSFWorkbook();

      //Creating a Spread Sheet
      XSSFSheet spreadsheet = workbook.createSheet("員工資料資訊");
      XSSFRow row = spreadsheet.createRow(1);
      XSSFCell cell;

      cell = row.createCell(1);
      cell.setCellValue("編號");

      cell = row.createCell(2);
      cell.setCellValue("姓名");

      cell = row.createCell(3);
      cell.setCellValue("年齡");

      cell = row.createCell(4);
      cell.setCellValue("薪水");

      cell = row.createCell(5);
      cell.setCellValue("部門");
      int i = 2;

      while(resultSet.next()) {
         row = spreadsheet.createRow(i);
         cell = row.createCell(1);
         cell.setCellValue(resultSet.getInt("emp_id"));

         cell = row.createCell(2);
         cell.setCellValue(resultSet.getString("name"));

         cell = row.createCell(3);
         cell.setCellValue(resultSet.getString("age"));

         cell = row.createCell(4);
         cell.setCellValue(resultSet.getString("salary"));

         cell = row.createCell(5);
         cell.setCellValue(resultSet.getString("dept"));
         i++;
      }

      FileOutputStream out = new FileOutputStream(
         new File("excel_from_database.xlsx"));

      workbook.write(out);
      out.close();

      System.out.println("excel_from_database.xlsx written successfully");
   }
}

執行上面範例程式碼,得到以下結果 -

excel_from_database.xlsx written successfully

建立的Excel檔案內容,如下所示 -