日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#創(chuàng)建Excel文件實(shí)例講解

從數(shù)據(jù)庫中導(dǎo)出C#創(chuàng)建Excel文件(含Interop.Excel.Dll)是我們在實(shí)際開發(fā)中遇到的問題,那么如何解決呢?是用什么方法可以實(shí)現(xiàn)呢,下面我們通過實(shí)例的演示向你具體介紹下。

創(chuàng)新互聯(lián)公司-云計(jì)算及IDC服務(wù)提供商,涵蓋公有云、IDC機(jī)房租用、成都移動機(jī)房、等保安全、私有云建設(shè)等企業(yè)級互聯(lián)網(wǎng)基礎(chǔ)服務(wù),服務(wù)熱線:18980820575

C#創(chuàng)建Excel文件實(shí)現(xiàn)實(shí)例:

 
 
 
  1. //創(chuàng)建一個(gè)excel application  
  2. Excel.Application xls_exp=null;  
  3. int rowindex=1;  
  4. int colindex=0;  
  5.  //創(chuàng)建一個(gè)workbook,一個(gè)worksheet  
  6. Excel._Workbook xls_book=null;  
  7. Excel._Worksheet xls_sheet=null;  
  8. try 
  9. {  
  10.  xls_exp=new Excel.ApplicationClass();  
  11.  xls_book=xls_exp.Workbooks.Add(true);  
  12. xls_sheet=(Excel._Worksheet)xls_book.ActiveSheet;  
  13. //C#創(chuàng)建Excel文件之取得數(shù)據(jù)  
  14.  DataTable aa=GetData();  
  15.  //將所得到的表的列名,賦值給單元格  
  16.  foreach(DataColumn col in aa.Columns)  
  17. {  
  18.  colindex++;  
  19.  xls_exp.Cells[1,colindex]=col.ColumnName;  
  20.  //水平對齊  
  21.  xls_sheet.get_Range(xls_exp.Cells[1,colindex],  
  22. xls_exp.Cells[1,colindex]).HorizontalAlignment=  
  23. Excel.XlVAlign.xlVAlignCenter;  
  24. //C#創(chuàng)建Excel文件之垂直對齊  
  25. xls_sheet.get_Range(xls_exp.Cells[1,colindex],  
  26. xls_exp.Cells[1,colindex]).VerticalAlignment=  
  27. Excel.XlVAlign.xlVAlignCenter;   
  28. //行高、列寬自適應(yīng)  
  29.  //xls_sheet.Cells.Rows.AutoFill();  
  30.  //xls_sheet.Cells.Columns.AutoFill();  
  31. }  
  32.  //同樣方法處理數(shù)據(jù)  
  33.  foreach(DataRow row in aa.Rows)  
  34. {  
  35. rowindex++;  
  36.  colindex=0;   
  37. foreach(DataColumn col in aa.Columns)  
  38. {  
  39. colindex++;  
  40. switch (row[col.ColumnName].GetType().ToString())  
  41. {  
  42. //字符  
  43. case ("System.String"):  
  44. //數(shù)字格式設(shè)置為文本  
  45. xls_sheet.get_Range(  
  46. xls_exp.Cells[rowindex,colindex],  
  47. xls_exp.Cells[rowindex,colindex]).NumberFormatLocal="@";  
  48. //C#創(chuàng)建Excel文件之水平對齊  
  49. xls_sheet.get_Range(  
  50. xls_exp.Cells[rowindex,colindex],  
  51. xls_exp.Cells[rowindex,colindex]).HorizontalAlignment=  
  52. Excel.XlVAlign.xlVAlignCenter;  
  53. //垂直對齊  
  54. xls_sheet.get_Range(  
  55. xls_exp.Cells[rowindex,colindex],  
  56. xls_exp.Cells[rowindex,colindex]).VerticalAlignment=  
  57. Excel.XlVAlign.xlVAlignCenter;   
  58. break;  
  59. //日期  
  60. case("System.DateTime"):  
  61.  //數(shù)字格式設(shè)置為yyyy-mm-dd hh:mm:ss日期  
  62. xls_sheet.get_Range(  
  63. xls_exp.Cells[rowindex,colindex],  
  64. xls_exp.Cells[rowindex,colindex]).  
  65. NumberFormatLocal="YYYY-MM-DD HH:MM:SS";  
  66. break;  
  67. }  
  68.  //C#創(chuàng)建Excel文件之給cell賦值  
  69. xls_exp.Cells[rowindex,colindex]=row[col.ColumnName];  
  70.  
  71. }  
  72. }  
  73.  //不可見,即后臺處理  
  74.  xls_exp.Visible=true;  
  75. catch(Exception err)  
  76. {  
  77.  MessageBox.show(err.Message);  
  78. }  
  79. //finally  
  80. //{  
  81.  //xls_exp.Quit();  
  82. //}  

C#創(chuàng)建Excel文件的基本內(nèi)容就向你介紹到這里,希望對你了解C#創(chuàng)建Excel文件有所幫助。


當(dāng)前標(biāo)題:C#創(chuàng)建Excel文件實(shí)例講解
轉(zhuǎn)載來源:http://m.5511xx.com/article/coedcpg.html