sql文件轉換為excel文件


最近經常需要把sql整理成excel,本人比較懶,所以寫一個小工具,用到了jxl包。以前沒有接觸過,正好了解一下。

一、基礎知識

      jxl操作excel包括對象 Workbook,Sheet,Cell。

     一個excel就對應一個Workbook對象,
     一個Sheet頁表就對應一個Sheet對象,
     一個單元格就對應一個Cell對象,
     一個Workbook可以有多個Sheet對象,
     一個Sheet對象可以有多個Cell對象。

二、 小工具

 1 package com.mq;
 2 
 3 
 4 import jxl.Workbook;
 5 import jxl.write.Label;
 6 import jxl.write.WritableSheet;
 7 import jxl.write.WritableWorkbook;
 8 import jxl.write.WriteException;
 9 import jxl.write.biff.RowsExceededException;
10 
11 import java.io.*;
12 import java.util.ArrayList;
13 
14 class SqlToExel {
15     public static void main(String[] args) {
16         String readPath ="F:\\0017.sql";
17         String writePath="F:\\0017.xls";
18         ArrayList<ArrayList<String>> allDate =getData(readPath);
19         writeExcel(writePath,allDate);
20     }
21     //讀取文件
22 private static ArrayList<ArrayList<String>> getData(String path){
23     //用於存儲所有從文件中讀取的數據
24     ArrayList<ArrayList<String >> allDate = new ArrayList<>();
25     try {
26         //字符緩沖輸入流
27         BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path))) ;
28         String line;
29         //存儲每一行要展示的內容
30         ArrayList<String> oneDate = new ArrayList<>();
31         //判斷是否讀到行尾
32         while((line = reader.readLine())!= null){
33             //判斷讀到的是否為空行
34             if(line.equals("")){
35                 allDate.add(oneDate);
36                 oneDate = new ArrayList<>();
37             }
38             //判斷讀到的內容是否為插入語句
39             if(line.indexOf("insert")!=-1){
40                oneDate.add(line);
41             }
42         }
43 
44     } catch (FileNotFoundException e) {
45         e.printStackTrace();
46     } catch (IOException e) {
47         e.printStackTrace();
48     }
49     return allDate;
50 }
51 
52 private  static void writeExcel(String path, ArrayList<ArrayList<String>> allDate){
53        if(path !=null && allDate !=null){
54            WritableWorkbook wwb = null;
55            WritableSheet ws;
56            String sheetName= "zhr";
57            try {
58                //首先要使用Workbook類的工廠方法創建一個可寫入的工作薄(Workbook)對象
59                    wwb = Workbook.createWorkbook(new File(path));
60                //創建sheet
61                    ws = wwb.createSheet(sheetName, 1);
62                //循環遍歷獲取內容
63                for(int i=0;i<allDate.size();i++){
64                    Label labelC;
65                    ArrayList<String> oneDate= allDate.get(i);
66                      String mergeDate = "";
67                      for (int j = 0; j < oneDate.size(); j++) {
68                          String oneLabelDate = oneDate.get(j);
69                          mergeDate = mergeDate + oneLabelDate + "\n";
70                      }
71                      labelC = new Label( 0, i, mergeDate);
72                       //將生成的單元格添加到工作表中
73                       ws.addCell(labelC);
74                   }
75                //寫入數據
76                wwb.write();
77 
78            } catch (IOException e) {
79                e.printStackTrace();
80            } catch (RowsExceededException e) {
81                e.printStackTrace();
82            } catch (WriteException e) {
83                e.printStackTrace();
84            } finally{
85                try {
86                    //關閉工作簿
87                    wwb.close();
88                } catch (WriteException e) {
89                    // TODO Auto-generated catch block
90                    e.printStackTrace();
91                } catch (IOException e) {
92                    // TODO Auto-generated catch block
93                    e.printStackTrace();
94                }
95            }
96        }
97    }
98 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM