将数据从 Windows Forms DataGridView 和 TextBox 导出到 Excel

Export data from Windows Forms DataGridView and TextBox to Excel

我有一个 Form,其中包含 TextBox 个控件和一个 DataGridView。我想将该表单中的数据导出到 excel 文件。 我正在使用这段代码,它非常适合 DataGridView 但我不知道如何导出 TextBox 控件数据。

private void copyAlltoClipboard()
{
    dataGridView1.SelectAll();
    DataObject dataObj = dataGridView1.GetClipboardContent();
    if (dataObj != null)
        Clipboard.SetDataObject(dataObj);
}
try
{
    copyAlltoClipboard();
    Microsoft.Office.Interop.Excel.Application xlexcel;
    Microsoft.Office.Interop.Excel.Workbook xlWorkBook 
    Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
    object misValue = System.Reflection.Missing.Value;
    xlexcel = new Microsoft.Office.Interop.Excel.Application();
    xlexcel.Visible = true;
    xlWorkBook = xlexcel.Workbooks.Add(misValue)
    xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
    Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1];
    CR.Select();

    xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);              
}
catch (Exception ee)
{
    MessageBox.Show(ee.Message);
}

您可以通过以下方式一一导出您的 LabelsTextBoxes 的值:

//Put Text of Label in Cell[1,1]
sheet.Cells[1, 1].Value = this.label1.Text;

//Put the Text of TextBox in Cell[1,2]
sheet.Cells[1, 2].Value = this.textBox1.Text;

然后把其他LabelsTextBoxes的内容放上去,最后把DataGridViewContents粘贴到合适的位置。

要使名称更短,请使用 using XL = Microsoft.Office.Interop.Excel;

这是代码

private void CopyGridToClipboard(DataGridView grid)
{
    //Exclude row headers
    grid.RowHeadersVisible = false;

    //Include column headers
    grid.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
    grid.SelectAll();
    DataObject dataObj = grid.GetClipboardContent();
    if (dataObj != null)
        Clipboard.SetDataObject(dataObj);

    //Set the visibility of row headers back
    grid.RowHeadersVisible = true;
}

private void button1_Click(object sender, EventArgs e)
{
    //Copy grid to clipboard
    this.CopyGridToClipboard(dataGridView1);

    //Open the excel application and add a workbook
    XL.Application application;
    XL.Workbook book;
    XL.Worksheet sheet;
    application = new XL.Application();
    application.Visible = true;
    book = application.Workbooks.Add();
    sheet = (XL.Worksheet)book.Worksheets[1];

    //label1 Text in Cell[1,1]
    ((XL.Range)sheet.Cells[1, 1]).Value = this.label1.Text;

    //textBox1 Text in Cell[1,2]
    ((XL.Range)sheet.Cells[1, 2]).Value = this.textBox1.Text;

    //label2 Text in Cell[2,1]
    ((XL.Range)sheet.Cells[2, 1]).Value = this.label2.Text;

    //textBox2 Text in Cell[2,2]
    ((XL.Range)sheet.Cells[2, 2]).Value = this.textBox2.Text;

    //Let row 3 empty
    //Paste grid into Cell[4,1]
    XL.Range gridRange = (XL.Range)sheet.Cells[4, 1];
    gridRange.Select();
    sheet.PasteSpecial(gridRange);
}

这是应用程序截图

这里是 excel 截图

备注

您还可以在方法末尾为单元格和区域添加格式:

sheet.Cells[1, 1].Font.Bold = true;
sheet.Cells[1, 1].Interior.Color = 
    System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Silver);

sheet.Cells[2, 1].Font.Bold = true;
sheet.Cells[2, 1].Interior.Color = 
    System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Silver);

sheet.Range[sheet.Cells[4, 1], 
            sheet.Cells[4, dataGridView1.ColumnCount]].Font.Bold = true;
sheet.Range[sheet.Cells[4, 1], 
            sheet.Cells[4, dataGridView1.ColumnCount]].Interior.Color = 
    System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Silver);