久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

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

 mylore 2011-11-29

通過DataSet導(dǎo)出Excel

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

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

  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("無法創(chuàng)建excel對象,,可能您的機(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. }   

 

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

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購買等信息,,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多