1
using System;
2
using System.Collections;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Web;
7
using System.Web.SessionState;
8
using System.Web.UI;
9
using System.Web.UI.WebControls;
10
using System.Web.UI.HtmlControls;
11
//*************************************
12
public class DataSetToExcel
13
{
14
public DataSetToExcel(){}
15
public void Convert(DataSet oDS,HttpResponse Response)
16
{
17
Response.Clear();
18
Response.Charset = "";
19
Response.ContentType = "application/vnd.ms-excel";
20
System.IO.StringWriter oSW = new System.IO.StringWriter();
21
HtmlTextWriter oHW = new HtmlTextWriter(oSW);
22
DataGrid oDG = new DataGrid();
23
oDG.DataSource = oDS.Tables[0];
24
oDG.DataBind();
25
oDG.RenderControl(oHW);
26
Response.Write(oSW.ToString());
27
Response.Flush();
28
Response.Close();
29
}
30
}
31
//*********************************************************
32
调用这个类就OK了
33
34
35
36
(2)
37
38
using System;
39
using System.Data;
40
using System.Data.OleDb;
41
namespace GRIS.ExcelReprot
42
{
43
/// <summary>
44
/// ImportExportToExcel 的摘要说明。
45
/// </summary>
46
public class ImportExportToExcel
47
{
48
private string strConn ;
49
50
private System.Windows.Forms.OpenFileDialog openFileDlg=new System.Windows.Forms.OpenFileDialog();
51
private System.Windows.Forms.SaveFileDialog saveFileDlg=new System.Windows.Forms.SaveFileDialog();
52
53
public ImportExportToExcel()
54
{
55
//
56
// TODO: 在此处添加构造函数逻辑
57
//
58
this.openFileDlg.DefaultExt = "xls";
59
this.openFileDlg.Filter = "Excel文件 (*.xls)|*.xls";
60
61
this.saveFileDlg.DefaultExt="xls";
62
this.saveFileDlg.Filter= "Excel文件 (*.xls)|*.xls";
63
64
}
65
66
从Excel文件导入到DataSet
125
126
从DataSet到出到Excel
198
199
从XML导入到Dataset
232
233
从DataSet导出到XML
271
}
272
}
273
274
public void ExportResult(DataSet ds)
275
{
276
HttpContext.Current.Response.Clear();
277
HttpContext.Current.Response.Charset = "";
278
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
279
StringWriter stringWrite = new StringWriter();
280
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
281
282
DataGrid dg = new DataGrid();
283
dg.DataSource = ds.Tables[0];
284
dg.DataBind();
285
dg.RenderControl(htmlWrite);
286
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=result.xls");
287
288
HttpContext.Current.Response.Write(stringWrite.ToString());
289
HttpContext.Current.Response.End();
290
}
291
292

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

125

126

198

199

232

233

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292
