C# 从sql server数据库导出固定格式的word文件

我有个word格式的文件,该文件的格式是固定的。如图

同时我的数据都存在sql server 2008中,大概有10万条左右数据,
请问如何通过C#实现 将数据库中的数据,导出到word的相应位置上,每个人生成一个word文旦。
【急】任何方法都行,只要能导出就行 ,否则我只能把数据库中的内容一条一条录入到word中了。。。。拜谢

第1个回答  2016-05-22
在word文档中插入内容的位置插入书签,保存为模板
循环数据表记录,使用模板生成新的word文档,定位到书签,替换值,保存
第2个回答  2014-03-28
OleDbDataAdapter adapter2 = new OleDbDataAdapter(strCommand2, conn2);
adapter2.Fill(ds2);
conn2.Close();

DataTable dt = ds2.Tables[0];
string[,] arr = new string[100, 15];
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
arr[i, j] = dt.Rows[i][j].ToString();
}
}
ToWord(arr, dt.Rows.Count);
this.Close();
}
private void ToWord(string[,] arr, int k)
{
string str = System.Environment.CurrentDirectory;
object docpath = str+"\\Files\\Normal\\Normal.doc";
object savepath = str+"\\Files\\testcases.doc";

app = new ApplicationClass();
doc = app.Documents.Open(ref docpath, ref isReadOnly, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing);
for (int p = 1; p <= k; p++)
{
doc.Tables[p].Select();
app.Selection.Copy();
object myunit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
app.Selection.EndKey(ref myunit, ref missing);
object pBreak = (int)Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
app.Selection.InsertBreak(ref pBreak);
app.Selection.Paste();
Table table = doc.Tables[p];
table.Cell(1, 2).Range.Text = arr[p - 1, 1];
table.Cell(1, 4).Range.Text = arr[p - 1, 2];
table.Cell(1, 6).Range.Text = arr[p - 1, 3];
table.Cell(2,2).Range.Text = arr[p - 1, 4];
table.Cell(2,4).Range.Text = arr[p - 1, 5];
table.Cell(3,2).Range.Text = arr[p - 1, 6];
table.Cell(4,2).Range.Text = arr[p - 1, 7];
table.Cell(5,1).Range.Text = arr[p - 1, 8];
table.Cell(6,1).Range.Text = arr[p - 1, 9];
table.Cell(7,1).Range.Text = arr[p - 1, 10];
}
doc.SaveAs(ref savepath, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing);
//doc.Close(ref missing, ref missing, ref missing);
app.Documents.Close();
第3个回答  2013-08-28
没接触过如何将数据库数据直接存成word形式,不过以前项目中看到有把数据库数据以流形式写到前端页面,当然这里Content-Type就不是以“text/html”形式写,而是另一种方式写,不过具体忘了是什么,好像是application/msword这种形式的。然后就可以保存成word了
第4个回答  2019-04-25
你在Word文档中设置一个宏,用VBA,去设定一个表的格式,然后你在C#中,接受数据库数据,然后将数据导入到VBA中就可以了
相似回答