免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
通過DataSet導(dǎo)出Excel .

通過DataSet導(dǎo)出Excel

分類: 開發(fā)積累 C# Winform 29人閱讀 評(píng)論(0) 收藏 舉報(bào)

   在以前的項(xiàng)目中,做這些類似的功能也挺多的,沒保存好,現(xiàn)在在這里記下,會(huì)以后所用:

  1. /// <summary>   
  2. /// 通過DataSet導(dǎo)出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//被點(diǎn)了取消    
  19.   
  20.     Excel.Application xlapp = new Excel.ApplicationClass();  
  21.   
  22.     if (xlapp == null)  
  23.     {  
  24.         MessageBox.Show("無(wú)法創(chuàng)建excel對(duì)象,可能您的機(jī)子未安裝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.     //寫入數(shù)值    
  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 = "正在導(dǎo)出數(shù)據(jù)["+ 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("文件已經(jīng)成功導(dǎo)出...");  
  86.         }  
  87.         catch (Exception ex)  
  88.         {  
  89.             filesaved = false;  
  90.             MessageBox.Show("導(dǎo)出文件時(shí)出錯(cuò),文件可能正被打開!/n" + ex.Message);  
  91.         }  
  92.     }  
  93.     else  
  94.     {  
  95.         filesaved = false;  
  96.     }  
  97.     xlapp.Quit();  
  98.     GC.Collect();//強(qiáng)行銷毀    
  99.   
  100. }   

 

這些代碼可以完成此功能,以后會(huì)繼續(xù)完善!

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
C#導(dǎo)入導(dǎo)出Excel通用類(SamWang)
C# 將數(shù)據(jù)導(dǎo)出到Excel匯總
讀寫excel
C# excel文件導(dǎo)入導(dǎo)出
c#操作Excel設(shè)置字體顏色
C#操作Excel類(通用?)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服