【问题标题】:Export Information from a Form to Excel将信息从表单导出到 Excel
【发布时间】:2017-05-12 03:28:38
【问题描述】:

我正在努力完成这部分工作,这是我目前的流程,请帮助我。

 private void copyAlltoClipboard()
    {
        G2.SelectAll();

        DataObject dataObj = G2.GetClipboardContent();
        if (dataObj != null)
            Clipboard.SetDataObject(dataObj);
    }

    private void btn_export_Click(object sender, EventArgs e)
    {
        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 Excel.Application();
        xlexcel.Visible = true;
        xlWorkBook = xlexcel.Workbooks.Add(misValue);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[1, 1];
        CR.Select();
        xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true); 
    }

所以这是代码,它将datagridview导出到excel(但没有标题文本,我还需要导出标题文本)。 而对于上面的所有文本框,我也希望它们导出到 Excel,我该怎么做呢?

我忘了说,这些文本框是只读的,是从数据表中的另一个表单调用的,不是用于输入的。

【问题讨论】:

    标签: c# excel winforms datagridview


    【解决方案1】:

    对于带有标题文本的Datagridview数据,您需要更改datagridview的ClipboardCopyMode

     dgv.ClipboardCopyMode =DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
    

    【讨论】:

      【解决方案2】:

      除了您获得代码的原始位置:Export the dataGridView to Excel with all the cells format

      您需要为主字段制作另一个 HTML 表,并将其与 DataGridView 的 HTML 表结合起来:

      private string ConvertMasterFieldsToHTMLTable()
      {
          StringBuilder sb = new StringBuilder();
          sb.AppendLine("<table border='1' cellpadding='0' cellspacing='0'>");
      
          sb.AppendLine("<tr><td>");
          sb.AppendLine("Purchaser: ");
          sb.AppendLine("</td><td>");
          sb.AppendLine(textBox1.Text);
          sb.AppendLine("</td></tr>");
      
          sb.AppendLine("<tr><td>");
          sb.AppendLine("Date: ");
          sb.AppendLine("</td><td>");
          sb.AppendLine(dateTimePicker1.Value.ToString());
          sb.AppendLine("</td></tr>");
      
          sb.AppendLine("<tr><td>");
          sb.AppendLine("PR: ");
          sb.AppendLine("</td><td>");
          sb.AppendLine(comboBox1.Text.ToString());
          sb.AppendLine("</td></tr>");
          sb.AppendLine("</table>");
          sb.AppendLine("<br>");
          return sb.ToString();
      }
      private void button1_Click(object sender, EventArgs e)
      {
          string fieldToHTMLTable = ConvertMasterFieldsToHTMLTable();
          string dgvToHTMLTable = ConvertDataGridViewToHTMLWithFormatting(dgv);
      
          //Strip the enclosing <HTML><body> tags and wrap them around both HTML Tables
          dgvToHTMLTable = dgvToHTMLTable.Replace("<html><body><center>", string.Empty);
          dgvToHTMLTable = dgvToHTMLTable.Replace("</center></body></html>", string.Empty);
      
          Clipboard.SetText("<html><body><center>"+ fieldToHTMLTable + dgvToHTMLTable + "</center></body></html>");
      }
      

      【讨论】:

      • string dgvToHTMLTable = ConvertDataGridViewToHTMLWithFormatting(dgv); 先生,这是从哪里来的?
      • 查看我链接到的另一个答案(问题在你的问题中有完全相同的代码),你需要在名为dgv的表单上有一个DataGridView控件
      • 一个明显的优化是使用TrimStart TrimEnd 而不是Replace,我会试着用这个做一个GitHub项目...
      【解决方案3】:

      我不知道它是否正确的解决方案,但它有效。

      只需添加以下函数(不要忘记将字符串值替换为相应的变量)。如果您想将数据移动到其他单元格或行,只需相应地更改制表符和换行符。

      private void PrefixOtherDataToClipboard()
      {
          StringBuilder str = new StringBuilder();
      
          //Replace appended strings with your corresponding variables
          str.Append("Purchaser\t" + "P1"); str.AppendLine("\tReject Reason\t" + "bla bla bla..");
          str.AppendLine("Date\t" + DateTime.Now);
          str.AppendLine("PR#\t" + "23432");
          str.AppendLine("Total Values\t" + "4234");
          str.AppendLine("Status\t" + "Waiting");
      
      
          str.AppendLine(Clipboard.GetText());
      
          Clipboard.SetText(str.ToString());            
      }
      

      现在在将网格数据复制到剪贴板后,在copyAlltoClipboard() 中调用此函数

      private void copyAlltoClipboard()
      {
          G2.SelectAll();
      
          DataObject dataObj = G2.GetClipboardContent();
          if (dataObj != null)
          {
              Clipboard.SetDataObject(dataObj);
              PrefixOtherDataToClipboard();
          }
      }
      

      注意:

      如果您为网格视图单元格设置了任何格式,则在我以文本(字符串)的形式从/向剪贴板读取和写入时,它不会被复制到 excel。

      【讨论】:

      • aww 我忘了提,这些文本框和只读的,是从数据表中的另一个表单调用的,而不是用于输入。
      • @Rajeev - 请参阅我在答案中引用的链接 - 它显示了如何以最快的方式保持格式和导出到 excel。
      猜你喜欢
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多