而從GridView的數據導出到Word文件,表格應在Word中創建,如何從GridView裏創建一個Word文檔中的表格與數據
表可能包含許多細胞,
在每個小區中,我需要存儲一個記錄從gridview的
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();
}
當心,這條線是錯誤的: 「sfd.Filter = 」Excel文檔格式(* .doc)|的* .doc「;」 Excel擴展名是xsl或xlsx,而不是doc – Oscar
它是word文檔「sfd.Filter =」word文檔(.doc)| .doc「;」 – user3445382