Java Jacob将数据库信息导入Word文档

package com.andy.jacob;
002
003 import java.io.File;
004 import java.sql.ResultSet;
005 import java.sql.ResultSetMetaData;
006 import java.sql.SQLException;
007 import java.util.ArrayList;
008 import java.util.List;
009
010 import com.andy.db.DBUtil;
011 import com.jacob.com.ComThread;
012
013
014 public class ReadOracleToWard {
015
016 private DBUtil dbUtil = null;
017 // 表格列名数组
018 private String colNames[] = null;
019 // 表格value值
020 private List colValuesList = null;
021
022 private Inerttable inerttable = null;
023
024
025 public void writeColNames(String colNames[]) {
026 for (int i = 0; i < colNames.length; i++) {
027 inerttable.putTxtToCell(1, 1, (i + 1), colNames[i]);
028 }
029 }
030
031
032 public void writeColValues(List colValuesList) {
033 String[] colValues = null;
034 int row = 2;
035 for (int i = 0; i < colValuesList.size(); i++) {
036 colValues = colValuesList.get(i);
037 for (int k = 0; k < colValues.length; k++) {
038 inerttable.putTxtToCell(1, row, (k + 1), colValues[k]);
039 }
040 row++;
041 }
042 }
043
044
045 public ResultSet getResultSet(String tableName) {
046 dbUtil = new DBUtil();
047 String sql = "select * from " + tableName;
048 ResultSet rs = null;
049
050 dbUtil.findConnection();
051 dbUtil.findPreparedStatement(sql);
052 rs = dbUtil.queryDB(sql);
053 System.out.println("getResultSet : rs : " + rs);
054 return rs;
055 }
056
057
058 public String[] getColNames(ResultSet rs) throws SQLException {
059 System.out.println("getResultSet : rs : " + rs);
060 ResultSetMetaData rsmd = null;
061 if (rs != null) {
062 rsmd = rs.getMetaData();
063 int count = rsmd.getColumnCount();
064
065 colNames = new String[count];
066
067 for (int i = 0; i < count; i++) {
068 colNames[i] = rsmd.getColumnName(i + 1);
069 }
070 } else {
071 colNames = new String[] {};
072 }
073 return colNames;
074 }
075
076
077 public List getColValuesList(ResultSet rs) throws SQLException {
078 colValuesList = new ArrayList();
079 System.out.println("getResultSet : rs : " + rs);
080 ResultSetMetaData rsmd = null;
081 if (rs != null) {
082 rsmd = rs.getMetaData();
083 int count = rsmd.getColumnCount();
084 String[] colValues = null;
085
086 while (rs.next()) {
087 colValues = new String[count];
088 for (int i = 0; i < count; i++) {
089 String value = rs.getString(i + 1);
090 // 将 数据库中的 null 转化为"null",避免在转化格式时出错
091 if (value == null || "".equals(value)) {
092 value = "null";
093 }
094 colValues[i] = value;
095 }
096 colValuesList.add(colValues);
097 }
098 }
099 return colValuesList;
100 }
101
102
103 public void writeToWord(String tableName) {
104 try {
105 long begin = System.currentTimeMillis();
106 ResultSet rs = getResultSet(tableName);
107 getColNames(rs);
108 getColValuesList(rs);
109
110 ComThread.InitSTA();
111 inerttable = new Inerttable();
112 inerttable.createNewDocument();
113 inerttable.createTable(colNames.length, colValuesList.size() + 1);
114
115 writeColNames(colNames);
116 writeColValues(colValuesList);
117
118 String docPath = getLocationPath(tableName);
119 inerttable.save(docPath);
120 ComThread.Release();
121
122 long end = System.currentTimeMillis();
123 System.out.println("用时:" + String.valueOf(end - begin));
124 } catch (Exception e) {
125 // TODO: handle exception
126 System.out.println("异常:" + e.getMessage());
127 } finally {
128 dbUtil.closeResource();
129 }
130 }
131
132
133 public String getLocationPath(String tableName) {
134 // 生成文件路径(在本项目src下)
135 File f = new File(".");
136 f = new File(f, "src/com/andy/jacob");
137
138 if (!f.exists()) {
139 f.mkdirs();
140 }
141 f = new File(f, tableName + ".docx");
142 return f.getAbsolutePath();
143 }
144
145 public static void main(String[] args) {
146 ReadOracleToWard toWard = new ReadOracleToWard();
147 String tableName = "andy_blob";
148 toWard.writeToWord(tableName);
149 }
150 }

[color=red]
此文档是别人写的!我拷贝下来的,方便以后观看!请勿怪![/color]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值