要求
環(huán)境信息:win2008server 開發(fā)工具:vs2015 開發(fā)語言:c#
要求:
1.點擊同步數(shù)據(jù)后接口獲取數(shù)據(jù)展示頁面同時過濾無效數(shù)據(jù)并寫入數(shù)據(jù)庫,數(shù)據(jù)可導(dǎo)出excel并支持分類導(dǎo)出
2.excel導(dǎo)入確認數(shù)據(jù),調(diào)用服務(wù)處理數(shù)據(jù)后寫入數(shù)據(jù)庫,并支持分類導(dǎo)出
這兩天搞了一個小功能,其他的不說了針對excel導(dǎo)入導(dǎo)出做一個小總結(jié)
導(dǎo)出文件
這里的文件導(dǎo)出是底層寫好的,個人理解有限而且畢竟屬于公司就不貼具體代碼了,簡單說一下思路
首先是建立導(dǎo)出excel管理類,用于管理excel文件導(dǎo)出的模板 樣式 每行的計算方式等等,當然需要在項目中添加該管理類的配置文件去匹配對應(yīng)模板;
1.讀取對應(yīng)配置文件,獲取配置文件模板信息 至于模板如何配置就不說啦xml文件操作園子里面很多篇關(guān)于這個文章
2.根據(jù)xml文件定義模板id遍歷查詢到該模板,這里有緩存機制為了可能處于兩方面考慮,1,防止頻繁讀取遍歷文件從而減少性能缺失 2.弱誤刪配置文件程序不會立即停止工作,監(jiān)控警報會首先暴露這個問題
3.最后就是讀取指定id下面的具體導(dǎo)出設(shè)置,比如標題頭,上限行數(shù),給定簡單默認值等等細節(jié)處理,同時也含有緩存機制
配置文件模板管理大致上有以上幾種功能,下面就是具體數(shù)據(jù)庫導(dǎo)出,還是那樣不能提供具體代碼但是思路可以說一說
導(dǎo)出管理類需要承接很多任務(wù),入數(shù)據(jù)庫查詢,數(shù)據(jù)過濾,導(dǎo)出格式控制,導(dǎo)出日志設(shè)置,導(dǎo)出預(yù)警等等
其實這并不是一個簡單的excel導(dǎo)出工具而是一個小型的導(dǎo)出平臺,承接一個導(dǎo)出實體去設(shè)置導(dǎo)出的各項數(shù)據(jù),但是還是需要開發(fā)者根據(jù)自己的需求去填寫相應(yīng)的模板,導(dǎo)出格式,數(shù)據(jù)驗證,數(shù)據(jù)查詢方式實體承接了導(dǎo)出方式
(xls或者csv等等很多格式)使用者只需要寫入具體導(dǎo)出db信息,以及導(dǎo)出表名稱和配置文件以及數(shù)據(jù)驗證就可以輕松使用它完成數(shù)據(jù)導(dǎo)出操作
并且,針對文件導(dǎo)出操作尤其是數(shù)據(jù)庫最好由底層實現(xiàn)通過response流發(fā)送到頁面執(zhí)行下載,數(shù)據(jù)相對規(guī)整一些如果在頁面直接執(zhí)行導(dǎo)出頁面有些太沉重了 畢竟這個是可以實現(xiàn)的
導(dǎo)入文件
導(dǎo)入文件首先需要上傳,文件上傳至服務(wù)器指定地址之后再去針對服務(wù)器文件解析,其實原理很簡單,就是通過解析上傳的文件通過oldb方式獲取解析后的文件dataset然后在寫入數(shù)據(jù)庫,下面是一個上傳文件格式驗證
具體的讀取方法就很簡單了 網(wǎng)上一搜一大把,不過要區(qū)分一下版本而且不同版本的excel文件行數(shù)上線不同下面貼一個我常用的excel到dataset方法
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
|
public static returnvalue readexceltodataset(string xlsfullfilename, bool ishdr, bool isimex, int limitsheetcount, bool isonlyverify) { returnvalue returnvalue = new returnvalue(); string fileext = uploadfileutils.getfileext(xlsfullfilename); if (string.isnullorempty(fileext) || !stringutils.isinlimitstr( "xls,xlsx" , fileext)) { returnvalue.haserror = true ; returnvalue.returncode = 1 ; returnvalue.message = "無效excel文件后綴" ; return returnvalue; } if (!file.exists(xlsfullfilename)) { returnvalue.haserror = true ; returnvalue.returncode = 2 ; returnvalue.message = "文件不存在" ; return returnvalue; } stringbuilder stringbuilder = new stringbuilder(); string str; if ( "xlsx" .equals(fileext, stringcomparison.currentcultureignorecase)) { stringbuilder.append( "provider=microsoft.ace.oledb.12.0" ); str = "excel 12.0;" ; } else { stringbuilder.append( "provider=microsoft.jet.oledb.4.0" ); str = "excel 8.0;" ; } stringbuilder.append( ";data source=" + xlsfullfilename); stringbuilder.append( ";extended properties=\"" + str); if (ishdr) { stringbuilder.append( ";hdr=yes" ); } if (isimex) { stringbuilder.append( ";imex=1" ); } stringbuilder.append( "\"" ); excelutils.log.debug(stringbuilder.tostring()); oledbconnection oledbconnection = new oledbconnection(stringbuilder.tostring()); try { oledbconnection.open(); datatable oledbschematable = oledbconnection.getoledbschematable(oledbschemaguid.tables, new object[] { null , null , null , "table" }); if (oledbschematable == null || oledbschematable.rows.count == 0 ) { returnvalue.haserror = true ; returnvalue.returncode = 3 ; returnvalue.message = "讀取不到sheet的信息" ; returnvalue result = returnvalue; return result; } if (isonlyverify) { returnvalue.haserror = false ; returnvalue.message = "讀取sheet信息正確" ; returnvalue.putvalue( "dtsheet" , oledbschematable); returnvalue result = returnvalue; return result; } int num = oledbschematable.rows.count; if (limitsheetcount > 0 && limitsheetcount < oledbschematable.rows.count) { num = limitsheetcount; } string[] array = new string[num]; for ( int i = 0 ; i < num; i++) { array[i] = oledbschematable.rows[i][ "table_name" ].tostring(); } dataset dataset = new dataset(); for ( int j = 0 ; j < num; j++) { string text = "select * from [" + array[j] + "]" ; excelutils.log.debug(text); oledbcommand selectcommand = new oledbcommand(text, oledbconnection); oledbdataadapter oledbdataadapter = new oledbdataadapter(selectcommand); datatable datatable = new datatable(array[j]); oledbdataadapter.fill(datatable); dataset.tables.add(datatable); } returnvalue.haserror = false ; returnvalue.putvalue( "dtsheet" , oledbschematable); returnvalue.putvalue( "arrtablename" , array); returnvalue.returnobject = dataset; returnvalue.message = "讀取成功" ; } catch (exception ex) { returnvalue.haserror = true ; returnvalue.returncode = - 100 ; returnvalue.returnexception = ex; returnvalue.message = ex.message; excelutils.log.warnformat( "readexceltodataset sbconn={0} 出錯,原因:{1}" , stringbuilder.tostring(), ex.message); } finally { oledbconnection.close(); } return returnvalue; } |
哦對 注意一下,如果用導(dǎo)出方法導(dǎo)出xls文件再用導(dǎo)入方法導(dǎo)入該文件會報錯的喲,我是默認保存.csv 實際就為了確定文件是否被使用過,所以當你的excel文件能導(dǎo)出單相同文件卻導(dǎo)入不了 請嘗試一下重新保存一下.xls格式 在進行導(dǎo)入
原文鏈接:http://www.cnblogs.com/workstation-liunianguowang/p/6606104.html