Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从windows应用程序导出word中的数据后如何设置其格式_C#_Sql Server 2008_Formatting_Ms Word - Fatal编程技术网

C# 从windows应用程序导出word中的数据后如何设置其格式

C# 从windows应用程序导出word中的数据后如何设置其格式,c#,sql-server-2008,formatting,ms-word,C#,Sql Server 2008,Formatting,Ms Word,大家好,从windows窗体应用程序导出数据后,请告诉我如何在word中格式化数据 private void button1_Click_1(object sender, EventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Word Documents (*.doc)|*.doc"; sfd.FileName = "WordFile.doc"; if (sfd.ShowDia

大家好,从windows窗体应用程序导出数据后,请告诉我如何在word中格式化数据

private void button1_Click_1(object sender, EventArgs e)
{
   SaveFileDialog sfd = new SaveFileDialog();

   sfd.Filter = "Word Documents (*.doc)|*.doc";

   sfd.FileName = "WordFile.doc";

   if (sfd.ShowDialog() == DialogResult.OK)
   {

      //ToCsV(dataGridView1, @"c:\export.xls");

      Toword(dataGridView1, sfd.FileName); // Here dvwACH is your grid view name

   }

}


private void Toword(DataGridView dGV, string filename)
{
   string stOutput = "";

   // Export titles:

   string sHeaders = "";

   for (int j = 0; j < dGV.Columns.Count; j++)
   {
      sHeaders = sHeaders.ToString() + Convert.ToString(dGV.Columns[j].HeaderText) + "\t";
   }
   stOutput += sHeaders + "\r\n";

   //Export data.

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

      for (int j = 0; j < dGV.Rows[i].Cells.Count; j++)
         stLine = stLine.ToString() + Convert.ToString(dGV.Rows[i].Cells[j].Value) + "\t";

      stOutput += stLine + "\r\n";


    }

    Encoding utf16 = Encoding.GetEncoding(1254);

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

    FileStream fs = new FileStream(filename, FileMode.Create);

    BinaryWriter bw = new BinaryWriter(fs);

    bw.Write(output, 0, output.Length); //write the encoded file

    bw.Flush();

    bw.Close();

    fs.Close();
 }`enter code here`