第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();