Winform exports the datagridview to excel, and winform exports the excel
If there is not much data, you can use the following method. If the data is large, it is not recommended to use this method.
If Microsoft. Office. Interop. Excel. dll does not exist on the computer, go to the DLL download site to download it.
Import the reference of this dll first.
Submit code:
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using Microsoft. office. interop. excel; using System. data. sqlClient; namespace export to execl {public partial class Form1: Form {public Form1 () {InitializeComponent () ;}// Bind the datagridview private void Bind () {string SQL = string. for Mat ("select id, no, name, age, gender, address from stuinfo"); // The dataset returned by the getSet method is required. tables [0] this. dataGridView1.DataSource = SQLHelpercs. getSet (SQLHelpercs. conString, SQL ). tables [0];} /// <summary> /// export excel /// </summary> /// <param name = "fileName"> name of the excel file to be saved </param> // /<param name = "dg"> name of the DataGridView </param> public void OutExecl (string fileName, dataGridView dg) {if (dg. rows. co Unt> 0) // checks whether the datagridview has data {string saveName = string. empty; // declare a save name SaveFileDialog sgfDialog = new SaveFileDialog (); // create a save object sgfDialog. defaultExt = "xls"; // Save the sgfDialog extension by default. filter = "Excel file | *. xls "; // type of the file to be saved sgfDialog. fileName = fileName; // Save the name sgfDialog. showDialog (); saveName = sgfDialog. fileName; if (saveName. indexOf (":") <0) return; // click Cancel Microsoft. office. interop. excel. application xlapp = New Microsoft. Office. Interop. Excel. Application (); if (xlapp = null) {MessageBox. Show ("cannot create Execl! "); Return;} Microsoft. office. interop. excel. workbooks wbs = xlapp. workbooks; Microsoft. office. interop. excel. workbook workbook = wbs. add (Microsoft. office. interop. excel. xlWBATemplate. xlWBATWorksheet); // Add a working thin Microsoft. office. interop. excel. worksheet worksheet = (Microsoft. office. interop. excel. worksheet) workbook. worksheets [1]; // get sheet1 // write the title for (int I = 0; I <dg. columnCount; I ++) {worksheet. cell S [1, I + 1] = dg. columns [I]. headerText;} // write value for (int r = 0; r <dg. rows. count; r ++) {for (int I = 0; I <dg. columnCount; I ++) {worksheet. cells [r + 2, I + 1] = dg. rows [r]. cells [I]. value;} System. windows. forms. application. doEvents ();} worksheet. columns. entireColumn. autoFit (); // adaptive if (saveName! = "") {Try {workbook. saved = true; workbook. saveCopyAs (saveName); // fileSaved = true;} catch (Exception ex) {// fileSaved = false; MessageBox. show ("An error occurred while exporting the file. The file may be opened! \ N "+ ex. Message) ;}} xlapp. Quit (); GC. Collect (); // The MessageBox. Show (fileName +" is saved successfully! "," Prompt ", MessageBoxButtons. OK);} else {MessageBox. Show (" the report is empty, no table needs to be exported "," prompt ", MessageBoxButtons. OK );}
} Private void Form1_Load (object sender, EventArgs e) {Bind ();} private void export to excelToolStripMenuItem_Click (object sender, EventArgs e) {OutExecl ("cool.xls", this. dataGridView1 );}}}