C中数据导出到Excel表格(9)
发布时间:2021-06-07
发布时间:2021-06-07
DataGridView 数据导出到Excel
31. System.Windows.Forms.SaveFileDialog saveDia = new SaveFileDialog();
32. saveDia.Filter = "Excel|*.xls";
33. saveDia.Title = "导出为Excel文件";
34. if(saveDia.ShowDialog()== System.Windows.Forms.DialogResult.OK
35. && !string.Empty.Equals(saveDia.FileName))
36. {
37. Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
38. Microsoft.Office.Interop.Excel.Workbook workbook =
workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
39. Microsoft.Office.Interop.Excel.Worksheet worksheet =
(Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
40. Microsoft.Office.Interop.Excel.Range range = null;
41. long totalCount = dt.Rows.Count;
42. long rowRead = 0;
43. float percent = 0;
44. string fileName = saveDia.FileName;
45. //写入标题
46. for (int i = 0; i < dt.Columns.Count; i++)
47. {
48. worksheet.Cells[1, i + 1] = dt.Columns[i].ColumnName;
49. range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1];
50. //range.Interior.ColorIndex = 15;//背景颜色
51. range.Font.Bold = true;//粗体
52. range.HorizontalAlignment =
Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;//居中
53. //加边框
54. range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous,
Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin,
Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null);
55. //range.ColumnWidth = 4.63;//设置列宽
56. //range.EntireColumn.AutoFit();//自动调整列宽
57. //r1.EntireRow.AutoFit();//自动调整行高
58. }
上一篇:红楼梦人物描写风格浅析
下一篇:美国联邦制的发展。