Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement Excel Import in C #

Shulou Source: shulou.com Published: 2022-06-02 09:07:54 09月26日 Update

Editor to share with you how to achieve Excel import C#, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

We should pay attention to the following points in the import of C # Excel:

1.C# Excel imports can only store 65535 rows of data. If your data is larger than 65535 rows, you need to split the excel and store it.

The garbled code imported by 2.C# Excel is mainly due to the problem of character setting.

1. Load Excel (read excel content) the return value is a DataSet

/ / load Excel public static DataSet LoadDataFromExcel (string filePath) {try {string strConn; strConn = "Provider=Microsoft.Jet. OLEDB.4.0;Data Source= "+ filePath +"; Extended Properties='Excel 8.0 FROM false IMEX 1'"; OleDbConnection OleConn = new OleDbConnection (strConn); OleConn.Open (); String sql =" SELECT * FROM [IMEX1 $] "; / / but change the Sheet name, such as sheet2, etc. OleDbDataAdapter OleDaExcel = new OleDbDataAdapter (sql, OleConn); DataSet OleDsExcle = new DataSet (); OleDaExcel.Fill (OleDsExcle," Sheet1 "); OleConn.Close (); return OleDsExcle } catch (Exception err) {MessageBox.Show ("data binding Excel failed! Reasons for failure: "+ err.Message,"prompt", MessageBoxButtons.OK, MessageBoxIcon.Information); return null;}}

2.C# Excel imports content. Parameter: excelTable is a table table to import excel.

Public static bool SaveDataTableToExcel (System.Data.DataTable excelTable, string filePath) {Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop. Excel.ApplicationClass (); try {app.Visible = false; Workbook wBook = app.Workbooks.Add (true); Worksheet wSheet = wBook.Worksheets [1] as Worksheet; if (excelTable.Rows.Count > 0) {int row = 0; row = excelTable.Rows.Count; int col = excelTable.Columns.Count; for (int I = 0; I < row; iTunes +) {for (int j = 0; j < col) String str = excelTable.Rows [I] [j] .ToString (); wSheet.Cells [I + 2, j + 1] = str;} int size = excelTable.Columns.Count; for (int I = 0; I < size; I +) {wSheet.Cells [1,1 + I] = excelTable. Columns [I] .ColumnName;} / / sets the query box app.DisplayAlerts = false; app.AlertBeforeOverwriting = false; / / Save workbook wBook.Save () that does not pop up to save and overwrite; / / Save excel file app.Save (filePath); app.SaveWorkspace (filePath); app.Quit (); app = null; return true;} catch (Exception err) {MessageBox.Show ("error exporting Excel! Error reasons: "+ err.Message,"prompt message", MessageBoxButtons.OK, MessageBoxIcon. Information); return false;} finally {} these are all the contents of the article "how to import Excel in C #". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Content clips data articles tips information reasons not so much garbled parameters names most characters documents more knowledge industry information information channels errors Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Microsoft Docker vpn MariaDB