C中数据导出到Excel表格(9)

发布时间: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. }

C中数据导出到Excel表格(9).doc 将本文的Word文档下载到电脑

精彩图片

热门精选

大家正在看

× 游客快捷下载通道(下载后可以自由复制和排版)

限时特价:7 元/份 原价:20元

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219