自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C# Excel導(dǎo)入相關(guān)知識(shí)總結(jié)

開(kāi)發(fā) 后端
Excel只能存儲(chǔ)65535行數(shù)據(jù),在使用C# Excel導(dǎo)入數(shù)據(jù)時(shí)需要注意這一問(wèn)題;另外,亂碼的問(wèn)題也值得我們關(guān)注。本文向您提供這兩種問(wèn)題的解決方法。

C# Excel導(dǎo)入有以下幾點(diǎn)需要我們注意:

1.C# Excel導(dǎo)入只能存儲(chǔ)65535行數(shù)據(jù),如果你的數(shù)據(jù)大于65535行,那么就需要將excel分割存放了。

2.C# Excel導(dǎo)入的亂碼,這主要是字符設(shè)置問(wèn)題。

1.加載Excel(讀取excel內(nèi)容)返回值是一個(gè)DataSet

  1. //加載Excel   
  2. public static DataSet LoadDataFromExcel  
  3. (string filePath)   
  4. {   
  5. try   
  6. {   
  7. string strConn;   
  8. strConn = "Provider=Microsoft.Jet.  
  9. OLEDB.4.0;Data Source=" +   
  10. filePath + ";Extended Properties='Excel   
  11. 8.0;HDR=False;IMEX=1'";   
  12. OleDbConnection OleConn =   
  13. new OleDbConnection(strConn);   
  14. OleConn.Open();   
  15. String sql = "SELECT * FROM    
  16. [Sheet1$]";//可是更改Sheet名稱(chēng),比如sheet2,等等   
  17.  
  18. OleDbDataAdapter OleDaExcel =   
  19. new OleDbDataAdapter(sql, OleConn);   
  20. DataSet OleDsExcle = new DataSet();   
  21. OleDaExcel.Fill(OleDsExcle, "Sheet1");   
  22. OleConn.Close();   
  23. return OleDsExcle;   
  24. }   
  25. catch (Exception err)   
  26. {   
  27. MessageBox.Show("數(shù)據(jù)綁定Excel失敗!  
  28. 失敗原因:" + err.Message, "提示信息",   
  29. MessageBoxButtons.OK, MessageBoxIcon.Information);   
  30. return null;   
  31. }   

2.C# Excel導(dǎo)入內(nèi)容,參數(shù):excelTable是要導(dǎo)入excel的一個(gè)table表

  1. public static bool SaveDataTableToExcel  
  2. (System.Data.DataTable excelTable,   
  3. string filePath)   
  4. {   
  5. Microsoft.Office.Interop.Excel.Application app =   
  6. new Microsoft.Office.Interop.  
  7. Excel.ApplicationClass();   
  8. try   
  9. {   
  10. app.Visible = false;   
  11. Workbook wBook = app.Workbooks.Add(true);   
  12. Worksheet wSheet =   
  13. wBook.Worksheets[1] as Worksheet;   
  14. if (excelTable.Rows.Count 〉0)   
  15. {   
  16. int row = 0;   
  17. row = excelTable.Rows.Count;   
  18. int col = excelTable.Columns.Count;   
  19. for (int i = 0; i < row; i++)   
  20. {   
  21. for (int j = 0; j < col; j++)   
  22. {   
  23. string str = excelTable.Rows[i][j].ToString();   
  24. wSheet.Cells[i + 2, j + 1] = str;   
  25. }   
  26. }   
  27. }   
  28.  
  29. int size = excelTable.Columns.Count;   
  30. for (int i = 0; i < size; i++)   
  31. {   
  32. wSheet.Cells[1, 1 + i] = excelTable.  
  33. Columns[i].ColumnName;   
  34. }   
  35. //設(shè)置禁止彈出保存和覆蓋的詢問(wèn)提示框   
  36. app.DisplayAlerts = false;   
  37. app.AlertBeforeOverwriting = false;   
  38. //保存工作簿   
  39. wBook.Save();   
  40. //保存excel文件   
  41. app.Save(filePath);   
  42. app.SaveWorkspace(filePath);   
  43. app.Quit();   
  44. app = null;   
  45. return true;   
  46. }   
  47. catch (Exception err)   
  48. {   
  49. MessageBox.Show("導(dǎo)出Excel出錯(cuò)!  
  50. 錯(cuò)誤原因:" + err.Message, "提示信息",   
  51. MessageBoxButtons.OK, MessageBoxIcon.  
  52. Information);   
  53. return false;   
  54. }   
  55. finally   
  56. {   
  57. }  

【編輯推薦】

  1. C#中定義裝箱和拆箱詳解
  2. 淺談C#類(lèi)型系統(tǒng)
  3. 三種不同的C#異常類(lèi)型
  4. 詳細(xì)介紹C#編譯器
  5. C#異常機(jī)制的相關(guān)解釋
責(zé)任編輯:冰荷 來(lái)源: hoopchina
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)