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

时间:2015-11-10 18:04:37

标签: c# excel winforms excel-interop

我有一个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);
}

1 个答案:

答案 0 :(得分:0)

您可以通过以下方式逐个导出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);
}

以下是应用程序截图

enter image description here

这是excel截图

enter image description here

注意

您还可以在方法结尾处向单元格和范围添加格式:

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);