1. 程式人生 > >Java呼叫POI匯出資料庫資訊

Java呼叫POI匯出資料庫資訊

資料庫連結包:連結:https://pan.baidu.com/s/18VhAHNoDF-gacq04AnBr4w 密碼:vjlr

POI呼叫包:連結:https://pan.baidu.com/s/1Jhz6HlJc9gutRBwWE4KU-g 密碼:dnzq


java程式碼:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;

public class WriteExcel {

    Connection ct = null;
    Statement stat = null;
    ResultSet rs = null;

    public void writeExcel(String finalXlsxPath) throws SQLException {
        OutputStream out = null;

        //第一步建立workbook
        HSSFWorkbook wb = new HSSFWorkbook();

        //第二步建立sheet
        HSSFSheet sheet = wb.createSheet("身份證錯誤資訊");

        //第三步建立行row:新增表頭0行
        HSSFRow row = sheet.createRow(0);
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);  //居中

        //第四步建立單元格
        HSSFCell cell = row.createCell(0);         //第一個單元格
        cell.setCellValue("賬戶");                  //設定值
        cell.setCellStyle(style);                   //內容居中

        cell = row.createCell(1);                   //第二個單元格   
        cell.setCellValue("密碼");
        cell.setCellStyle(style);

        cell = row.createCell(2);                   //第三個單元格  
        cell.setCellValue("學號");
        cell.setCellStyle(style);

        cell = row.createCell(3);                   //第四個單元格  
        cell.setCellValue("姓名");
        cell.setCellStyle(style);

        cell = row.createCell(4);                   //第四個單元格  
        cell.setCellValue("學院");
        cell.setCellStyle(style);

        try {
            /**
             * 往Excel中寫新資料
             */
            ResultSet rs = init("");

            int j = 1;
            while (rs.next()) {
                row = sheet.createRow(j++);
                //建立單元格並且新增資料
                row.createCell(0).setCellValue(rs.getString(1));
                row.createCell(1).setCellValue(rs.getString(2));
                row.createCell(2).setCellValue(rs.getString(3));
                row.createCell(3).setCellValue(rs.getString(4));
                row.createCell(4).setCellValue(rs.getString(5));
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.flush();
                    out.close();
                    out = null;
                }
                if (rs != null) {
                    rs.close();
                    rs = null;
                }
                if (stat != null) {
                    stat.close();
                    stat = null;
                }
                if (ct != null) {
                    ct.close();
                    ct = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //第六步將生成excel檔案儲存到指定路徑下
        try {
            FileOutputStream fout = new FileOutputStream(finalXlsxPath);
            wb.write(fout);
            fout.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("Excel檔案生成成功...");
    }

    /**
     * 判斷Excel的版本,獲取Workbook
     *
     * @return
     * @throws IOException
     */
    public Workbook getWorkbok(File file) throws IOException {
        Workbook wb = null;
        FileInputStream in = new FileInputStream(file);
        wb = new HSSFWorkbook(in);
        return wb;
    }

    //初始化
    public ResultSet init(String sql) {
        if (sql.equals("")) {
            sql = "select * from login_student";
        }
        try {
            //1.載入驅動
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("載入成功");
            //2.連線資料庫
            //定義幾個常量
            String url = "jdbc:mysql://localhost:3306/system_test";
            String user = "root";
            String passwd = "next123456";

            ct = DriverManager.getConnection(url, user, passwd);
            stat = ct.createStatement();//建立stat物件
            rs = stat.executeQuery(sql);//查詢結果

            return rs;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}