Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Excel Import in C #

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 294

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report