【发布时间】:2015-11-10 18:04:37
【问题描述】:
我有一个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);
}
【问题讨论】:
-
EPPlus 是您最好的选择:nuget.org/packages/EPPlus
-
你需要添加sn-ps的代码和你目前尝试过的。
-
你可以看到我正在使用的代码
标签: c# excel winforms excel-interop