鍍金池/ 教程/ Linux/ Apache POI數(shù)據(jù)庫
Apache POI電子表格/Spreadsheet
Apache POI公式
Apache POI打印區(qū)域
Apache POI - Java Excel APIs
Apache POI數(shù)據(jù)庫
Apache POI單元格/Cells
Apache POI環(huán)境設(shè)置
Apache POI字體/Fonts
Apache POI超鏈接
POI教程
POI核心類
Apache POI工作簿

Apache POI數(shù)據(jù)庫

本章介紹了POI庫與數(shù)據(jù)庫的交互方式。有了JDBC幫助,可以從數(shù)據(jù)庫中檢索數(shù)據(jù)并插入數(shù)據(jù)來使用POI庫電子表格。讓我們考慮SQL操作MySQL數(shù)據(jù)庫。

寫入數(shù)據(jù)庫

讓我們假設(shè)數(shù)據(jù)表是 emp_tbl 存有雇員信息是從MySQL數(shù)據(jù)庫 test 中檢索。

EMP ID EMP NAME DEG SALARY DEPT
1201 Gopal Technical Manager 45000 IT
1202 Manisha Proof reader 45000 Testing
1203 Masthanvali Technical Writer 45000 IT
1204 Kiran Hr Admin 40000 HR
1205 Kranthi Op Admin 30000

使用下面的代碼從數(shù)據(jù)庫中檢索數(shù)據(jù),并插入到同一個電子表格。

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 ExcelDatabase 
{
   public static void main(String[] args) throws Exception 
   {
      Class.forName("com.mysql.jdbc.Driver");
      Connection connect = DriverManager.getConnection( 
      "jdbc:mysql://localhost:3306/test" , 
      "root" , 
      "root"
      );
      Statement statement = connect.createStatement();
      ResultSet resultSet = statement
      .executeQuery("select * from emp_tbl");
      XSSFWorkbook workbook = new XSSFWorkbook(); 
      XSSFSheet spreadsheet = workbook
      .createSheet("employe db");
      XSSFRow row=spreadsheet.createRow(1);
      XSSFCell cell;
      cell=row.createCell(1);
      cell.setCellValue("EMP ID");
      cell=row.createCell(2);
      cell.setCellValue("EMP NAME");
      cell=row.createCell(3);
      cell.setCellValue("DEG");
      cell=row.createCell(4);
      cell.setCellValue("SALARY");
      cell=row.createCell(5);
      cell.setCellValue("DEPT");
      int i=2;
      while(resultSet.next())
      {
         row=spreadsheet.createRow(i);
         cell=row.createCell(1);
         cell.setCellValue(resultSet.上一篇:Apache POI打印區(qū)域下一篇:Apache POI超鏈接