【发布时间】: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();
}
【问题讨论】: