C中数据导出到Excel表格(10)
发布时间:2021-06-07
发布时间:2021-06-07
DataGridView 数据导出到Excel
59. //写入内容
60. for (int r = 0; r < dt.DefaultView.Count; r++)
61. {
62. for (int i = 0; i < dt.Columns.Count; i++)
63. {
64. worksheet.Cells[r + 2, i + 1] = dt.DefaultView[r][i];
65. range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[r + 2, i + 1];
66. range.Font.Size = 9;//字体大小
67. //加边框
68. range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous,
Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin,
Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null);
69. range.EntireColumn.AutoFit();//自动调整列宽
70. }
71. rowRead++;
72. percent = ((float)(100 * rowRead)) / totalCount;
73. System.Windows.Forms.Application.DoEvents();
74. }
75. range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizont
al].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;
76. if (dt.Columns.Count > 1)
77. {
78. range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].
Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;
79. }
80. try
81. {
82. workbook.Saved = true;
83. workbook.SaveCopyAs(fileName);
84. }
85. catch (Exception ex)
86. {
87. //lblMsg.Text = "导出文件时出错,文件可能正被打开!/n" + ex.Message;
88. MessageBox.Show( "导出文件时出错,文件可能正被打开!/n" + ex.Message );
89. return;
上一篇:红楼梦人物描写风格浅析
下一篇:美国联邦制的发展。