C # Data Center reconstruction-export an excel table

Source: Internet
Author: User

C # Data Center reconstruction-export an excel table
Preface

In data center reconstruction, a user-friendly function is to export data to excel and summarize it.

Code
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using Microsoft. office. interop. excel; using Microsoft. office. core; using System. windows. forms; using System. data. sqlClient; using System. data. oleDb; using System. threading; namespace UI {public class Excel {public void RExcel (string name, DataGridView dgv) {// total number of visible columns, total number of visible rows int colCount = dgv. colu Mns. getColumnCount (maid. visible); int rowCount = dgv. rows. getRowCount (maid. visible); // if (dgv. rows. count = 0 | rowCount = 0) {MessageBox. show ("no data in the table", "prompt");} else {// select the path to the created file SaveFileDialog save = new SaveFileDialog (); save. filter = "excel files (*. xlsx) | *. xlsx "; save. title = "select the location of the data to be exported"; save. fileName = name + DateTime. now. T OLongDateString (); if (save. showDialog () = DialogResult. OK) {string fileName = save. fileName; // MessageBox. show (save. fileName); // create an Excel Object Microsoft. office. interop. excel. application excel = new Microsoft. office. interop. excel. application (); if (excel = null) {MessageBox. show ("Excel cannot be started", "prompt"); return ;}// create an Excel worksheet Microsoft. office. interop. excel. workbook excelBook = excel. workbooks. add (true ); Microsoft. office. interop. excel. worksheet excelSheet = (Microsoft. office. interop. excel. worksheet) excelBook. worksheets [1]; // excel. application. workbooks. add (true); // generate the field name int k = 0; for (int I = 0; I <dgv. columnCount; I ++) {if (dgv. columns [I]. visible) // do not export hidden columns {excel. cells [1, k + 1] = dgv. columns [I]. headerText; k ++ ;}// fill in the data for (int I = 0; I <dgv. rowCount; I ++) {k = 0; for (int j = 0; J <dgv. columnCount; j ++) {if (dgv. columns [j]. visible) // do not export hidden columns {if (dgv [j, I]. valueType = typeof (string) {excel. cells [I + 2, k + 1] = "" + dgv [j, I]. value. toString ();} else {excel. cells [I + 2, k + 1] = dgv [j, I]. value. toString () ;}} k ++ ;}} try {excelBook. saved = true; excelBook. saveCopyAs (fileName); MessageBox. show ("exported successfully! ");} Catch {MessageBox. Show (" Export failed, file may be in use "," prompt ");}}}}}}
The above is in our method class, in the interface, if we need to use this method:

 

 

private void btnExcel_Click(object sender, EventArgs e){    string name = "";    Excel ex = new Excel();    ex.RExcel(name,dataGridView1);}
Summary

Although this code was used in the system after I found it online and modified it, I am not sure exactly how it runs, but it is just the beginning, these things will become vulnerable after our research.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.