1
public static void GridViewToExcel(Stream myStream, DevExpress.XtraGrid.Views.Grid.GridView dataGridView1)
2
{
3
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
4
string str = "";
5
try
6
{
7
//寫(xiě)標(biāo)題
8
for (int i = 1; i < dataGridView1.Columns.Count; i++)
9
{
10
if (!string.IsNullOrEmpty(dataGridView1.Columns[i].Caption))
11
{
12
if (i > 1)
13
{
14
str += "\t";
15
}
16
str += dataGridView1.Columns[i].Caption;
17
}
18
}
19
20
sw.WriteLine(str);
21
//寫(xiě)內(nèi)容
22
for (int j = 0; j < dataGridView1.RowCount; j++)
23
{
24
string tempStr = "";
25
for (int k = 1; k < dataGridView1.Columns.Count; k++)
26
{
27
if (!string.IsNullOrEmpty(dataGridView1.Columns[k].Caption))
28
{
29
if (k > 1)
30
{
31
tempStr += "\t";
32
}
33
tempStr += dataGridView1.GetRowCellValue(j, dataGridView1.Columns[k].FieldName);
34
}
35
}
36
sw.WriteLine(tempStr);
37
}
38
sw.Close();
39
myStream.Close();
40
}
41
catch (Exception ex)
42
{
43
MessageBox.Show(ex.ToString());
44
}
45
finally
46
{
47
sw.Close();
48
myStream.Close();
49
}
50
51
}

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
