加急!!关于输出word字体的问题!!!!!!!!一行中包含不同字体

weixin_40850180 2017-10-30 03:25:01
各位大哥,帮帮手!

想要从dataGridView中输出到word,dataGridView有三列,在word中合并成一行,但要是这三列的内容用不同字体和颜色,如何实现?


需要变成下面的样子

其中山为宋体,其他为黑体

求大哥们帮忙

...全文
246 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_40850180 2017-11-01
  • 打赏
  • 举报
回复
引用 5 楼 qq_28194303 的回复:
我不知道你到底要的什么效果,但是以我看 你可以把你的数据集放在这样的一个二维数组里

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

        }
    }
出来的效果不就是这样了吗
多谢 ! 我试试看,也许能有做到!
晨易夕 2017-10-31
  • 打赏
  • 举报
回复
稍微封装一下不就可以了吗,怎么实现不了呢
晨易夕 2017-10-31
  • 打赏
  • 举报
回复
我不知道你到底要的什么效果,但是以我看
你可以把你的数据集放在这样的一个二维数组里

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

}
}


出来的效果不就是这样了吗
weixin_40850180 2017-10-31
  • 打赏
  • 举报
回复
引用 3 楼 qq_28194303 的回复:
稍微封装一下不就可以了吗,怎么实现不了呢
新手一个,刚刚起步,还不会封装, 只会简单的使用循环,大哥给我说个思路,我研究研究?
weixin_40850180 2017-10-30
  • 打赏
  • 举报
回复
引用 1 楼 qq_28194303 的回复:
我通过录制宏来试了一下,C#代码

            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");          
效果 是不是你要的效果我就不知道咯。 想要实现自己的效果自己录宏来看看吧。
我试试,这个方法尝试过了,我想要的是针对一整列,修改直接用text的字体能做到,但是需要放在循环一整列的时候就不能做到
晨易夕 2017-10-30
  • 打赏
  • 举报
回复
我通过录制宏来试了一下,C#代码

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");


效果

是不是你要的效果我就不知道咯。
想要实现自己的效果自己录宏来看看吧。

111,096

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧