打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
通过DataSet导出Excel .

通过DataSet导出Excel

分类: 开发积累 C# Winform 29人阅读 评论(0) 收藏 举报

   在以前的项目中,做这些类似的功能也挺多的,没保存好,现在在这里记下,会以后所用:

  1. /// <summary>   
  2. /// 通过DataSet导出Excel   
  3. /// </summary>   
  4. /// <param name="ds"></param>   
  5. public static void ExporDataSetToExcel(DataSet ds)  
  6. {  
  7.     if (ds == nullreturn;  
  8.   
  9.     string savefilename = "";  
  10.     bool filesaved = false;  
  11.   
  12.     SaveFileDialog savedialog = new SaveFileDialog();  
  13.     savedialog.DefaultExt = "xls";  
  14.     savedialog.Filter = "excel文件|*.xls";  
  15.     savedialog.FileName = "sheet1";  
  16.     savedialog.ShowDialog();  
  17.     savefilename = savedialog.FileName;  
  18.     if (savefilename.IndexOf(":") < 0) return//被点了取消    
  19.   
  20.     Excel.Application xlapp = new Excel.ApplicationClass();  
  21.   
  22.     if (xlapp == null)  
  23.     {  
  24.         MessageBox.Show("无法创建excel对象,可能您的机子未安装excel");  
  25.         return;  
  26.     }  
  27.   
  28.     Excel.Workbooks workbooks = xlapp.Workbooks;  
  29.     Excel._Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);  
  30.     Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];//取得sheet1    
  31.     Excel.Range range;  
  32.   
  33.     //   string oldcaption=this.captiontext;    
  34.     long totalcount = ds.Tables[0].Rows.Count;  
  35.     long rowread = 0;  
  36.     float percent = 0;  
  37.   
  38.     //   worksheet.Cells[1,1]=this.captiontext;    
  39.     //写入字段    
  40.     for (int i = 0; i < ds.Tables[0].Columns.Count; i++)  
  41.     {  
  42.         worksheet.Cells[2, i + 1] = ds.Tables[0].Columns[i].ColumnName;  
  43.         range = (Excel.Range)worksheet.Cells[2, i + 1];  
  44.         range.Interior.ColorIndex = 15;  
  45.         range.Font.Bold = true;  
  46.   
  47.     }  
  48.     //写入数值    
  49.     //   this.captionvisible = true;    
  50.     for (int r = 0; r < ds.Tables[0].Rows.Count; r++)  
  51.     {  
  52.         for (int i = 0; i < ds.Tables[0].Columns.Count; i++)  
  53.         {  
  54.             worksheet.Cells[r + 3, i + 1] = ds.Tables[0].Rows[r][i];  
  55.         }  
  56.         rowread++;  
  57.         percent = ((float)(100 * rowread)) / totalcount;  
  58.         //    this.captiontext = "正在导出数据["+ percent.tostring("0.00") +"%]";    
  59.         System.Windows.Forms.Application.DoEvents();  
  60.     }  
  61.     //   this.captionvisible = false;    
  62.     //   this.captiontext = oldcaption;    
  63.   
  64.     range = worksheet.get_Range(worksheet.Cells[2, 1], worksheet.Cells[ds.Tables[0].Rows.Count + 2, ds.Tables[0].Columns.Count]);  
  65.     range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null);  
  66.   
  67.     range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;  
  68.     range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = Excel.XlLineStyle.xlContinuous;  
  69.     range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight = Excel.XlBorderWeight.xlThin;  
  70.   
  71.     if (ds.Tables[0].Columns.Count > 1)  
  72.     {  
  73.         range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;  
  74.         range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous;  
  75.         range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;  
  76.     }  
  77.   
  78.     if (savefilename != "")  
  79.     {  
  80.         try  
  81.         {  
  82.             workbook.Saved = true;  
  83.             workbook.SaveCopyAs(savefilename);  
  84.             filesaved = true;  
  85.             MessageBox.Show("文件已经成功导出...");  
  86.         }  
  87.         catch (Exception ex)  
  88.         {  
  89.             filesaved = false;  
  90.             MessageBox.Show("导出文件时出错,文件可能正被打开!/n" + ex.Message);  
  91.         }  
  92.     }  
  93.     else  
  94.     {  
  95.         filesaved = false;  
  96.     }  
  97.     xlapp.Quit();  
  98.     GC.Collect();//强行销毁    
  99.   
  100. }   

 

这些代码可以完成此功能,以后会继续完善!

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
C#导入导出Excel通用类(SamWang)
C# 将数据导出到Excel汇总
读写excel
C# excel文件导入导出
c#操作Excel设置字体颜色
C#操作Excel类(通用?)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服