从Windows窗体DataGridView和TextBox导出数据到Excel
我有一个包含数量的TextBox
控件和DataGridView
的Form
。 我想从该表单导出数据到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); }
您可以通过这种方式逐个导出Labels
和TextBoxes
的值:
//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;
然后放置其他Labels
和文本TextBoxes
的内容,最后将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); }
这里是应用程序的截图
这里是最好的截图
注意
您也可以在方法的末尾添加格式到单元格和范围:
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);