111,096
社区成员




public class MyColumn
{
private string _text = "";
private string _fontName = "宋体";
private Microsoft.Office.Interop.Word.WdColor _fontColor = Microsoft.Office.Interop.Word.WdColor.wdColorBlack;
private float _fontSize = 15;
public string Text { get { return _text; } set { _text = value; } }
public string FontName { get { return _fontName; } set { _fontName = value; } }
public Microsoft.Office.Interop.Word.WdColor FontColor { get { return _fontColor; } set { _fontColor = value; } }
public float FontSize { get { return _fontSize; } set { _fontSize = value; } }
}
MyColumn[,] dt = new MyColumn[5,3];
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 3; j++)
{
string fontName = "宋体";
MSW.WdColor color = MSW.WdColor.wdColorBlack;
if (j == 2)
{
fontName = "黑体";
color = MSW.WdColor.wdColorRed;
}
MyColumn col = new MyColumn { Text=""+i+j, FontName=fontName,FontColor=color};
dt[i,j] = col;
}
}
WordHelper.ExportText(dt);
public class WordHelper
{
public static void ExportText(MyColumn[,] _data)
{
object filePath = "e:\\test.doc";
MSW.Application mswApp = new MSW.Application();
MSW.Document mswDoc = mswApp.Documents.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Object Nothing = Missing.Value;
int l = _data.GetLength(0);
int z = _data.GetLength(1);
for (int i = 0; i < _data.GetLength(0); i++)
{
for(int j = 0; j < _data.GetLength(1); j++)
{
mswDoc.Application.Selection.Font.Name = _data[i, j].FontName;
mswDoc.Application.Selection.Font.Size = _data[i, j].FontSize;
mswDoc.Application.Selection.Font.Color = _data[i, j].FontColor;
mswDoc.Application.Selection.TypeText(_data[i, j].Text);
mswDoc.Application.Selection.TypeText("\t");
}
mswDoc.Application.Selection.TypeText("\n");
}
object format = MSW.WdSaveFormat.wdFormatDocument;
mswDoc.SaveAs(ref filePath, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
mswDoc.Close(ref Nothing, ref Nothing, ref Nothing);
mswApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}
}
mswDoc.Application.Selection.Font.Name = "黑体";//字体
mswDoc.Application.Selection.Font.Color = MSW.WdColor.wdColorBlue;
mswDoc.Application.Selection.TypeText("测试文本1\t");
mswDoc.Application.Selection.Font.Name = "幼圆";//字体
mswDoc.Application.Selection.Font.Color = MSW.WdColor.wdColorRed;
mswDoc.Application.Selection.TypeText("测试文本2\t");
mswDoc.Application.Selection.Font.Name = "宋体";//字体
mswDoc.Application.Selection.Font.Color = MSW.WdColor.wdColorGreen;
mswDoc.Application.Selection.TypeText("测试文本3\t");