关于wpf中把datagrid导出为word表格文件怎么控制每行的宽度
代码如下
urlstring为我的word模板路径
fileName为文件导出后保存的路径
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = app.Documents.Add(urlstring);
app = doc.Application;
doc.ActiveWindow.Visible = true;
foreach (Microsoft.Office.Interop.Word.Bookmark bk in doc.Bookmarks)
{
object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;
object missing = System.Reflection.Missing.Value;
if (bk.Name == "neirong")
{
Microsoft.Office.Interop.Word.Range range = bk.Range;
range.Tables.Add(range, dataGrid.Items.Count + 1, 9);
Microsoft.Office.Interop.Word.Table tb = range.Tables[1];
tb.set_Style("网格型");
tb.Cell(1, 1).Range.Text = "事件类型";
tb.Cell(1, 2).Range.Text = "发生日期";
tb.Cell(1, 3).Range.Text = "事件经过";
tb.Cell(1, 4).Range.Text = "整改措施";
tb.Cell(1, 5).Range.Text = "等级";
tb.Cell(1, 6).Range.Text = "科室";
tb.Cell(1, 7).Range.Text = "上报人";
tb.Cell(1, 8).Range.Text = "相关患者姓名";
tb.Cell(1, 9).Range.Text = "相关住院号";
DataTable dt = (dataGrid.ItemsSource as DataView).Table;
int i = 0;
foreach (DataRow dr in dt.Rows)
{
tb.Cell(i + 2, 1).Range.Text = dr["NOTE"].ToString();
tb.Cell(i + 2, 2).Range.Text = dr["HAPPENDATE"].ToString();
tb.Cell(i + 2, 3).Range.Text = dr["COURSEEVENT"].ToString();
tb.Cell(i + 2, 4).Range.Text = dr["MEASURE"].ToString();
tb.Cell(i + 2, 5).Range.Text = dr["THINGTYPE"].ToString();
tb.Cell(i + 2, 6).Range.Text = dr["DEPARTNAME"].ToString();
tb.Cell(i + 2, 7).Range.Text = dr["REPORTNAME"].ToString();
tb.Cell(i + 2, 8).Range.Text = dr["PATNAME"].ToString();
tb.Cell(i + 2, 9).Range.Text = dr["PATIENT_ID"].ToString();
i++;
}
}
}
string savaPath = fileName + "--" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".docx";
doc.SaveAs(savaPath);
我想自定义每列的宽度而不是每列平均分配