【问题标题】:To display each exported record in each cell in table in word file在 Word 文件的表格中的每个单元格中显示每个导出的记录
【发布时间】:2014-03-26 06:31:29
【问题描述】:

我已将数据从 gridview 导出到与 gridview 中相同的单词。

但我需要在导出时在 word 表格中的不同单元格中显示每条记录

M 在 dot net 中使用 windows 应用程序

我从 gridview 导出数据时得到了输出 ------账单明细-----

ID 名称位置
1 rasdf 自卫队
2 自卫队 jlkj
3 dfdf dfdf
4 dsaf sdfdsfds
5 孟买国王

这就像一个有 4 列的表格.....我需要这样的输出

1 rasdf sdf 2 sdf jlkj 3 dfdf dfdf 4 dsaf sdfdsfds
5 孟买国王

这段代码写在导出按钮中

private void Eportto work(object sender, EventArgs e)
        {
            sfd.Filter = "Excel Documents (*.doc)|*.doc";

            if (sfd.ShowDialog() == DialogResult.OK)
             {
              ExportToWord(); 
                } 
        }


private void ExportToWord()
        {

            string strForPrint = "";


                string strHeaderTitle = "";

                for (int j = 0; j < dataGridView1.Columns.Count; j++)
                {
                    strHeaderTitle = strHeaderTitle.ToString() + Convert.ToString(dataGridView1.Columns[j].HeaderText) + "\t\t";
                }

                strForPrint += strHeaderTitle + "\r\n";

                // writing datagridview data.

                for (int i = 0; i < dataGridView1.RowCount - 1; i++)
                {
                    string strLineData = "";

                    for (int j = 0; j < dataGridView1.Rows[i].Cells.Count; j++)
                    {

                        strLineData = strLineData.ToString() + Convert.ToString(dataGridView1.Rows[i].Cells[j].Value);
                        if (j == 1)
                        {
                            strLineData = strLineData + "\t\t\t";
                        }
                        else
                        {
                            strLineData = strLineData + "\t\t";
                        }
                    }

                    strForPrint += strLineData + "\r\n";
                }


            Encoding utf16 = Encoding.GetEncoding(1254);


            byte[] output = utf16.GetBytes(strForPrint);

            FileStream fs = new FileStream(sfd.FileName, FileMode.Create);

            BinaryWriter bw = new BinaryWriter(fs);

          bw.Write(output, 0, output.Length); //write data into file
          MessageBox.Show("File Created.....");

            bw.Flush();

            bw.Close();

            fs.Close();
        }

【问题讨论】:

    标签: c# sql button gridview


    【解决方案1】:

    尝试用这个例子来实现:

    为了从 C# windows 应用程序将数据插入到单词表中,您可以参考下面共享的链接:

    msdn link

    【讨论】:

      【解决方案2】:

      更改 strForPrint += strLineData + "\r\n"; --> for (int i = 0; i 的 for 循环内部

      到

       If(strForPrint%4==0)//4 rows has been added
       {
         strForPrint += strLineData + "\r\n"; //go to new line
       }
       else {
        strLineData = strLineData + "\t\t"; //4 rows not added stay on same line
       }
      

      希望这个逻辑清楚

      【讨论】:

      • 1 rasdf sdf 2 sdf jlkj 3 dfdf dfdf 4 dsaf sdfdsfds 5 孟买国王
      • 我需要一个 word 表格,将每条记录存储在一个单元格中......表格包含 4 列和 N 行,因为我们没有记录。如果假设我有 6 条记录。在第一行将存储 4 条记录,其余 2 条将显示在占据该行前两个单元格的第 2 行
      猜你喜欢
      • 1970-01-01
      • 2018-01-20
      • 1970-01-01
      • 2014-01-04
      • 1970-01-01
      • 2020-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多