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)