C# 如何使用gridview中的数据在word文档中创建表
将数据从gridview导出到word文件时,应在word中创建一个表 表可能包含许多单元格 在每个单元格中,我需要存储一条从gridview导出的记录C# 如何使用gridview中的数据在word文档中创建表,c#,gridview,word-automation,C#,Gridview,Word Automation,将数据从gridview导出到word文件时,应在word中创建一个表 表可能包含许多单元格 在每个单元格中,我需要存储一条从gridview导出的记录 I have exported the data from gridview to word, like this 1 rasdf sdf 2 sdf jlkj 3 dfdf dfdf 4
I have exported the data from gridview to word,
like this
1 rasdf sdf
2 sdf jlkj
3 dfdf dfdf
4 dsaf sdfdsfds
5 king mumbai
but i need the output in table format in which Each record should be in one cell, second cell contains another records
![OUTput view is attached with this link][1]
这是我写的代码
private void button5_Click(object sender, EventArgs e)
{
// Table table = new table();
sfd.Filter = "Excel Documents (*.doc)|*.doc";
if (sfd.ShowDialog() == DialogResult.OK)
{
ExportToWord();
}
}
private void ExportToWord()
{
string strForPrint = "";
//writing bill fields
// strForPrint += "Bill No : " + txtBillNo.Text + "\t";
//strForPrint += "Date : " + dtpDate.Text + "\r\n\r\n";
//strForPrint += "Customer Name : " + txtCustomer.Text + "\r\n\r\n";
//strForPrint += "Remarks : " + txtRemarks.Text + "\r\n\r\n\r\n";
strForPrint += "-----Bill Detail-----" + "\r\n\r\n\r\n";
// writing datagridview column titles:
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();
}
private void按钮5_单击(对象发送者,事件参数e)
{
//Table Table=新表();
sfd.Filter=“Excel文档(*.doc)|*.doc”;
if(sfd.ShowDialog()==DialogResult.OK)
{
ExportToWord();
}
}
私有无效ExportToWord()
{
字符串strForPrint=“”;
//写入账单字段
//strForPrint+=“票据编号:”+txtBillNo.Text+“\t”;
//strForPrint+=“日期:”+dtpDate.Text+“\r\n\r\n”;
//strForPrint+=“客户名称:”+txtCustomer.Text+“\r\n\r\n”;
//strForPrint+=“备注:”+txtComments.Text+“\r\n\r\n\r\n”;
strForPrint+=“----账单明细------”+“\r\n\r\n\r\n”;
//写入datagridview列标题:
字符串strHeaderTitle=“”;
对于(int j=0;j
这是一种非常丑陋的创建Word文档的方法。。即使它可以工作,您也无法控制似乎需要的格式。我强烈建议使用一些库来创建真正的word文档,如WordDocumentGenerator或类似的
另一种方法是使用XSL模板来转换从数据库获得的xml数据
我喜欢,可以通过NuGet获得,而且非常容易使用。注意,这一行是错误的:“sfd.Filter=“Excel文档(.doc)|.doc”;“Excel扩展名是xsl或xlsx,而不是docYa它是word文档“sfd.Filter=“word文档(.doc)|.doc”;”