C# 在word文件的表格中的每个单元格中显示每个导出的记录
我已将数据从gridview导出到word,与gridview中的数据相同 但我需要在导出时在word中显示表中不同单元格中的每条记录 M在dotnet中使用windows应用程序 我在从gridview导出数据时得到了输出 ---账单明细--- ID名称位置 1 rasdf sdf 2 sdf jlkj 3 dfdf dfdf 4个dsaf SDFDSFD 5孟买国王酒店 这是一张有4个柱的桌子。。。。。我需要这样的输出 1个雷达自卫队2个雷达自卫队jlkj 3个雷达自卫队4个雷达自卫队SDFDSFD 5孟买国王酒店 这段代码是用导出按钮编写的 尝试使用此示例实现: 要从C windows应用程序将数据插入word表格,您可以参考下面共享的链接:C# 在word文件的表格中的每个单元格中显示每个导出的记录,c#,sql,button,gridview,C#,Sql,Button,Gridview,我已将数据从gridview导出到word,与gridview中的数据相同 但我需要在导出时在word中显示表中不同单元格中的每条记录 M在dotnet中使用windows应用程序 我在从gridview导出数据时得到了输出 ---账单明细--- ID名称位置 1 rasdf sdf 2 sdf jlkj 3 dfdf dfdf 4个dsaf SDFDSFD 5孟买国王酒店 这是一张有4个柱的桌子。。。。。我需要这样的输出 1个雷达自卫队2个雷达自卫队jlkj 3个雷达自卫队4个雷达自卫队SDF
更改strForPrint+=strLineData+\r\n;内部为->的循环,内部为int i=0;i
希望这个逻辑是清楚的1 rasdf sdf 2 sdf jlkj 3 DFDFDF dfdf 4 dsaf sdfdsfds 5 king mumbaiI需要一个word表格,将每条记录存储在一个单元格中……表格包含4列和N行,因为我们没有记录。如果我有6张唱片。在第一行中,将存储4条记录,其余2条记录将显示在占用该行前两个单元格的第二行上
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();
}
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
}