Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将数据从Windows窗体DataGridView和TextBox导出到Excel_C#_Excel_Winforms_Excel Interop - Fatal编程技术网

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

C# 将数据从Windows窗体DataGridView和TextBox导出到Excel,c#,excel,winforms,excel-interop,C#,Excel,Winforms,Excel Interop,我有一个表单,其中包含许多文本框控件和一个数据网格视图。我想将该表单中的数据导出到excel文件中。 我正在使用这段代码,它非常适合DataGridView但我不知道如何导出TextBox控件数据 private void copyAlltoClipboard() { dataGridView1.SelectAll(); DataObject dataObj = dataGridView1.GetClipboardContent(); if (dataObj != nul

我有一个
表单
,其中包含许多
文本框
控件和一个
数据网格视图
。我想将该表单中的数据导出到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);
}

您可以通过以下方式逐个导出
标签
文本框
的值:

//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;
然后将其他
标签
文本框
的内容放入,最后将
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屏幕截图

注意

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);
}
您还可以在方法末尾向单元格和区域添加格式:

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

您可以通过以下方式逐个导出
标签
文本框
的值:

//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;
然后将其他
标签
文本框
的内容放入,最后将
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屏幕截图

注意

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);
}
您还可以在方法末尾向单元格和区域添加格式:

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

EPPlus是您的最佳选择:您需要添加代码片段以及迄今为止所尝试的内容。您可以看到我正在使用的代码。Applus是您的最佳选择:您需要添加代码片段以及迄今为止所尝试的内容。您可以看到我正在使用的代码