C # Export the data displayed in DataGridView to Excel (Super Utility Edition)

Source: Internet
Author: User

Using system;using system.collections.generic;using system.linq;using system.text;using Microsoft.Office.Interop; Using microsoft.office.interop.excel;using system.windows.forms;using Excel = Microsoft.Office.Interop.Excel; public class exprottoexcel{public void Datatoexcel (DataGridView dgv,toolstripprogressbar tempprogressbar,toolstripst Atuslabel ToolStrip) {if (DGV.        Rows.Count = = 0) {MessageBox.Show ("no Data"); return;        } MessageBox.Show ("Start generating data to be exported", "Export hint", MessageBoxButtons.OK, MessageBoxIcon.Information);        Excel.Application Excel = new Excel.Application (); Excel.        APPLICATION.WORKBOOKS.ADD (TRUE); Excel.           Visible = false; for (int i = 0; i < DGV. ColumnCount; i++) Excel. Cells[1, i + 1] = DGV. Columns[i].        HeaderText;        Tempprogressbar.visible = true;        Tempprogressbar.minimum = 1; Tempprogressbar.maximum = DGV.        RowCount;        Tempprogressbar.step = 1; ToolStrip.  Visible = true;      for (int i = 0; i < DGV. RowCount; i++) {for (int j = 0; J < DGV. ColumnCount; J + +) {if (dgv[j, I]. ValueType = = typeof (String)) {Excel. Cells[i + 2, j + 1] = "'" + dgv[j, I].                Value.tostring (); } else {Excel. Cells[i + 2, j + 1] = Dgv[j, I].                Value.tostring (); }} ToolStrip. Text = "| | Status: Generating section "+i+"/"+DGV".            Rowcount+ "a";        Tempprogressbar.value = i + 1; } ToolStrip. Text = "| | Status: Build Succeeded!        "; MessageBox.Show ("Build succeeded, save.        "," Generate hint ", messageboxbuttons.ok,messageboxicon.information); Excel.    Visible = true; }}

C # Export the data displayed in DataGridView to Excel (Super Utility Edition)

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.