您的位置:首页 > 其它

【原】winform高效导出Excel带格式设置

2007-12-27 15:26 211 查看
参考网上的方法修改,1000条记录导出只要3S(1GRDM,C2.8CPU).

1.项目添加excel对象类库的引用,Microsoft Excel 11.0 object library(不同版本的Excel,类库不同,这是2003的)

2.代码

//-***************获取要写入excel的数据源***************

2 Dao model=new Dao();

3 DataTable dt=model.GetFileNameList(0,intPageSize,Convert.ToDateTime(dtPFrom.Value), Convert.ToDateTime(dtPTo.Value));//取得dataGrid绑定的DataSet

4 if(dt==null) return;

5 DataGridTableStyle ts = dataGrid1.TableStyles[0];

6

7

8 //-***************获取excel对象***************

9 string saveFileName="";

10 bool fileSaved=false;

11 SaveFileDialog saveDialog=new SaveFileDialog();

12 saveDialog.DefaultExt ="xls";

13 saveDialog.Filter="Excel文件|*.xls";

14 saveDialog.FileName ="导入记录查询结果 "+DateTime.Today.ToString("yyyy-MM-dd");

15 saveDialog.ShowDialog();

16 saveFileName=saveDialog.FileName;

17 if(saveFileName.IndexOf(":")<0) return; //被点了取消

18 Excel.Application xlApp=new Excel.Application();

19 if(xlApp==null)

20 Excel.Workbook workbook = xlApp.Workbooks.Add(true);

25 Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];

26 Excel.Range range;

27

28

29 string oldCaption=dataGrid1.CaptionText;

30 // 列索引,行索引,总列数,总行数

31 int colIndex = 0;

32 int RowIndex = 0;

33 int colCount = ts.GridColumnStyles.Count;

34 int RowCount=dt.Rows.Count;

35

36

37 // *****************获取数据*********************

38 dataGrid1.CaptionVisible = true;

39 dataGrid1.CaptionText = "正在导出数据";

40 // 创建缓存数据

41 object[,] objData = new object[RowCount + 1, colCount];

42 // 获取列标题

43 foreach(DataGridColumnStyle cs in ts.GridColumnStyles)

44

48 // 获取具体数据

49 for(RowIndex =1;RowIndex< RowCount;RowIndex++)

50

58 //********************* 写入Excel*******************

59

60 range = worksheet.get_Range(xlApp.Cells[2,1],xlApp.Cells[RowCount,colCount]);

61 range.Value2= objData;

62 Application.DoEvents();

63

64 //*******************设置输出格式******************************

65

66 //设置顶部説明

67 range = worksheet.get_Range(xlApp.Cells[1,1],xlApp.Cells[1,colCount]);

68 range.MergeCells = true;

69 range.RowHeight=38;

70 range.Font.Bold=true;

71 range.Font.Size=14;

72 range.Font.ColorIndex=10;//字体颜色

73 xlApp.ActiveCell.FormulaR1C1 = "导入记录查询结果";

74

75 //特殊数字格式

76 range = worksheet.get_Range(xlApp.Cells[2,colCount],xlApp.Cells[RowCount,colCount]);

77 range.NumberFormat="yyyy-MM-dd hh:mm:ss";

78

79 xlApp.Cells.HorizontalAlignment=Excel.Constants.xlCenter;

80 range = worksheet.get_Range(xlApp.Cells[2,1],xlApp.Cells[2,colCount]);

81 range.Interior.ColorIndex = 10;//背景色

82 range.Font.Bold = true;

83 range.RowHeight=20;

84 ((Excel.Range)worksheet.Cells[2,1]).ColumnWidth=25;

85 ((Excel.Range)worksheet.Cells[2,2]).ColumnWidth=13;

86 ((Excel.Range)worksheet.Cells[2,3]).ColumnWidth=18;

87 ((Excel.Range)worksheet.Cells[2,4]).ColumnWidth=15;

88 ((Excel.Range)worksheet.Cells[2,5]).ColumnWidth=22;

89

90 //***************************保存**********************

91 dataGrid1.CaptionVisible = false;

92 dataGrid1.CaptionText = oldCaption;

93 if(saveFileName!="")

94 else

xlApp.Quit();

GC.Collect();//强行销毁

TimeSpan dateEnd=new TimeSpan(DateTime.Now.Ticks);

TimeSpan tspan=dateBegin.Subtract(dateEnd).Duration();

MessageBox.Show(tspan.ToString());

if(fileSaved && File.Exists(saveFileName))

System.Diagnostics.Process.Start(saveFileName);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: