1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
| package jay;
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Pattern;
import static java.lang.System.out; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRichTextString; 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.hssf.util.HSSFColor;
public class CheckData {
static { try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } }
static void run(String db, boolean only_chinese) throws Exception { Map<String, List<String>> map = new HashMap<String, List<String>>();
BufferedReader reader = new BufferedReader(new FileReader(db + ".txt")); String line;
while ((line = reader.readLine()) != null) { List<String> list = new ArrayList<String>(); String[] parts = line.split("="); String table = parts[0]; String fields = parts[1]; for (String filed : fields.split(",")) { list.add(filed.trim()); } map.put(table, list); }
for (String key : map.keySet()) { out.println("table: " + key + ", " + map.get(key)); } reader.close();
genData(db, map, only_chinese); }
static void genData(String db, Map<String, List<String>> map, boolean only_chinese) { Connection conn = null;
try { conn = DriverManager .getConnection("jdbc:sqlserver://DbServer:1433;integratedSecurity=true;DatabaseName=" + "Public_" + db); } catch (SQLException e) { e.printStackTrace(); }
HSSFWorkbook wb = new HSSFWorkbook(); HSSFFont f = wb.createFont(); f.setFontHeightInPoints((short) 12); f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle cs = wb.createCellStyle(); cs.setFont(f);
int tables = 0; for (String table : map.keySet()) { listField(table, map.get(table), wb, cs, conn, only_chinese); tables++; } out.println("共有" + tables + "个表的数据输出。");
try { String fileName = db + ".xls"; if (only_chinese) { fileName = db + "-only-chinese.xls"; }
FileOutputStream out = new FileOutputStream(fileName); wb.write(out); out.close(); } catch (Exception e) { e.printStackTrace(); }
try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } }
static void listField(String table, List<String> options, HSSFWorkbook wb, HSSFCellStyle cs, Connection conn, boolean only_chinese) {
HSSFSheet sheeet = wb.createSheet(table); int row_index = 0; HSSFRow row = sheeet.createRow(row_index++); for (int i = 0; i < options.size(); i++) { HSSFCell cell = row.createCell(i); cell.setCellValue(options.get(i)); cell.setCellStyle(cs); }
String selectOption = ""; for (int i = 0; i < options.size() - 1; i++) { selectOption += options.get(i) + ","; } selectOption += options.get(options.size() - 1);
Statement stmt; final String regexp = "[\u4e00-\u9fa5]+"; final Pattern pattern = Pattern.compile(regexp);
try { stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select " + selectOption + " from " + table); int limit = 65534; boolean needCreate = false; int sheetIndex = 2; while (rs.next()) { if (row_index > limit && row_index <= limit * 2) { needCreate = true; if (sheetIndex >= 5) { out.println("table: " + table + " 超出最大数量" + sheetIndex + ",不再计算"); break; } }
if (needCreate) { out.println("table: " + table + "(" + sheetIndex + ")" + " 生成"); sheeet = wb.createSheet(table + "(" + sheetIndex + ")"); sheetIndex++; row_index = 1; needCreate = false; }
if (only_chinese) { boolean hasChinese = false;
for (int i = 0; i < options.size(); i++) { String value = rs.getString(i + 1); if (value != null) if (pattern.matcher(value).find()) { hasChinese = true; break; } } if (hasChinese) { row = sheeet.createRow(row_index++); for (int i = 0; i < options.size(); i++) { String value = rs.getString(i + 1); HSSFCell cell = row.createCell(i); cell.setCellValue(value); } } } else { row = sheeet.createRow(row_index++); for (int i = 0; i < options.size(); i++) { String value = rs.getString(i + 1); HSSFCell cell = row.createCell(i); cell.setCellValue(value); } } } } catch (SQLException e) { e.printStackTrace(); } }
static void xlsTest() throws Exception { HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet("sheet1");
HSSFFont f = wb.createFont(); f.setFontHeightInPoints((short) 12); f.setColor(HSSFColor.RED.index); f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle cs = wb.createCellStyle(); cs.setFont(f);
for (short rownum = (short) 0; rownum < 30; rownum++) { HSSFRow row = s.createRow(rownum); for (short cellnum = (short) 0; cellnum < 10; cellnum += 2) { HSSFCell cell2 = row.createCell(cellnum + 1);
cell2.setCellValue(new HSSFRichTextString("Hello! " + cellnum)); cell2.setCellStyle(cs); } }
FileOutputStream out = new FileOutputStream("workbook.xls"); wb.write(out); out.close(); }
static void getMessageTest() throws Exception { Connection conn = null;
final String regexp = "[\u4e00-\u9fa5]+"; final Pattern pattern = Pattern.compile(regexp);
try { conn = DriverManager .getConnection("jdbc:sqlserver://DbServer:1433;integratedSecurity=true;DatabaseName=" + "Public_X");
Statement stmt = conn.createStatement(); ResultSet rs = stmt .executeQuery("select dbo.big5ISOToUnicode(dbo.UnicodeToGbkISO(message)) from X where id = 4775"); while (rs.next()) { String value = rs.getString(1); out.println("<< " + value); if (pattern.matcher(value).find()) { out.println("find it..."); }
}
rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } }
public static void main(String[] args) throws Exception { run("XDataBase", false); run("XDataBase", true); } }
|