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 }

猜你喜欢

转载自www.cnblogs.com/muqingzhi123/p/9641839.html
今日推荐