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 write Excel Operation in C #

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to write Excel operation of C#". In the daily operation, I believe that many people have doubts about how to write Excel operation of C#. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to write Excel operation of C#". Next, please follow the editor to study!

Problems with the write Excel operation of the C# operation Excel:

The excel file can only store 65535 rows of data. If your data is larger than 65535 rows, you need to split the excel and store it. two。 With regard to garbled codes, this is mainly a matter of character setting.

C # operates Excel to write Excel content. Parameter: excelTable is a table table to be imported into excel.

Public static bool SaveDataTableToExcel (System.Data.DataTable excelTable, string filePath) {Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.ApplicationClass (); try {/ / C # operation Excel write Excel operation 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; jacks +) {string str = excelTable.Rows [I] [j] .ToString (); wSheet.Cells [I + 2, j + 1] = str;} / / C # operation Excel write Excel operation int size = excelTable.Columns.Count; for (int I = 0; I < size False; app.AlertBeforeOverwriting +) {wSheet.Cells [1,1 + I] = excelTable.ColumnName;} / / set the query prompt box app.DisplayAlerts = false; app.AlertBeforeOverwriting = false; / / Save the workbook wBook.Save (); / / Save the excel file app.Save (filePath); app.SaveWorkspace (filePath); app.Quit (); app = null; return true } catch (Exception err) {MessageBox.Show ("error exporting Excel! The reason for the error: + err.Message, "prompt", MessageBoxButtons.OK, MessageBoxIcon.Information); return false;} finally {} / / write Excel operation of C# operation Excel} at this point, the study on "how to write Excel operation in C#" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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: 0

*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