C# 导出到Word中的四种格式
(2013-08-23 13:01:57)
标签:
标题
数据
格式
四种
单元格
it
|
分类:
C#
|
///
/// 导出Word
///
/// 类容
/// 列数
public static void OutPutWordDT(List strContent, int Col)
{
Object Nothing = System.Reflection.Missing.Value;
Application oword = new Application();
Document odoc = oword.Documents.Add(ref Nothing, ref Nothing, ref
Nothing, ref Nothing);
try
{
int Rows = strContent.Count % Col;
if (Rows != 0)
Rows = Rows + 1;
else
Rows = strContent.Count / Col;
odoc.Paragraphs.Alignment =
WdParagraphAlignment.wdAlignParagraphCenter;
Table otable = odoc.Tables.Add(oword.Selection.Range, Rows, Col,
ref Nothing, ref Nothing);
otable.Range.ParagraphFormat.Alignment =
WdParagraphAlignment.wdAlignParagraphLeft;//设置对其方式
otable.Borders.OutsideLineStyle =
WdLineStyle.wdLineStyleSingle;//设置表格边框样式
//otable.Cell(1, 1).Merge(otable.Cell(2, 1)); //合并单元格
int index = 0;
for (int i = 1; i <= Rows; i++)
{
for (int j = 1; j <= Col && index < strContent.Count;
j++, index++)
{
otable.Cell(i, j).Range.Text = strContent[index];
otable.Cell(i, j).Range.Borders.OutsideLineStyle =
WdLineStyle.wdLineStyleSingle;//设置单元格样式
}
}
oword.Visible = true;