将数据从Windows窗体DataGridView和TextBox导出到Excel [英] Export data from Windows Forms DataGridView and TextBox to Excel
问题描述
我有一个表单
,其中包含 TextBox
控件和 DataGridView
。我想将该表单中的数据导出到excel文件。
我正在使用这个代码,它对于 DataGridView
完美工作,但我不知道如何导出 TextBox
控制数据。
I have a Form
that contains number of TextBox
controls and a DataGridView
. I want to export the data from that form to an excel file.
I am using this code it work perfectly for DataGridView
but I don't know how to export TextBox
controls data.
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);
}
推荐答案
您可以导出您的标签
和 TextBoxes
一个一个这样:
You can export the value of your Labels
and TextBoxes
one by one this way:
//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;
然后将其他标签
和 TextBoxes
,最后将 DataGridViewContents
贴在合适的位置。
Then put the content of other Labels
and TextBoxes
and at last paste the DataGridViewContents
at suitable position.
使用XL = Microsoft.Office.Interop.Excel;
To keep names shorter use 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截图
注意
您还可以在方法结束时向单元格和范围添加格式:
You can also add formatting to cells and ranges at the end of method:
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);
这篇关于将数据从Windows窗体DataGridView和TextBox导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!