如何使用C#在Excelcell中多色文本 [英] How do I multicolor a text in an Excelcell with C#

查看:132
本文介绍了如何使用C#在Excelcell中多色文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#中编写了一个应用程序,它将一些数据(字符串)写入excel文档。我需要在一个单元格中写几个字符串。在此之后,我尝试着色这些字符串。在我的示例中,有四个不同的字符串需要在一个单元格中以不同方式着色。第一个应该是蓝色,第二个应该是绿色,第三个应该是黑色,最后一个应该是红色。当我在示例中尝试它时,总是第一个和最后一个是正确的,但所有其他都是错误的。对于这个项目,我使用的是Windows 7 Professional,Microsoft Excel 2010,Visual Studio 2012 Ultimate。如何修复我的来源?



这是我的来源:



I have written an application in c# which writes some data (strings) into an excel-document. I need to write several strings into one cell. After this I try to color these strings. In my example there are four different strings that need to be colored differently in one cell. The first one should be blue, the second one should be green, the third one should be black and the last one should be red. When I try it as in the example, always the first and the last one are correct, but all the others are false. For this project I am using Windows 7 Professional, Microsoft Excel 2010, Visual Studio 2012 Ultimate. How can I fix my source?

This is my Source:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Excel;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        private Microsoft.Office.Interop.Excel.Application excel = null;
        private Microsoft.Office.Interop.Excel.Workbook workbook = null;
        public Microsoft.Office.Interop.Excel.Worksheet _sheet = null;
        public Form1()
        {
            myMethod();
        }

        private void myMethod()
        {
            excel = new Microsoft.Office.Interop.Excel.ApplicationClass();

            object missing = Missing.Value;
            excel.Workbooks.Add(missing);
            workbook = excel.ActiveWorkbook;
            workbook.Worksheets.Add(missing, missing, missing);

            _sheet =
                    (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
            _sheet.Name = "Sheet";

            string[] part = { "exampleBluee", "exampleGreen", "exampleBlack", "exampleReddd" };
            string[] faults = { "f1", "f2", "f3", "f4" };
            _sheet.Columns.WrapText = true;

            Microsoft.Office.Interop.Excel.Range position = _sheet.get_Range("A1");
            position.VerticalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter;
            for (int i = 0; i < 4; i++)
            {
                position.Value2 += part[i] + " ";
                int fehler = i;

                // default color
                var myColor = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Pink);

                if (faults[fehler] == "f1")
                {
                    myColor = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.RoyalBlue);
                }
                else if (faults[fehler] == "f2")
                {
                    myColor = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Green);
                }
                else if (faults[fehler] == "f3")
                {
                    myColor = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
                }
                else if (faults[fehler] == "f4")
                {
                    myColor = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
                }

                var nLenTotal = position.Value2.ToString().Length;
                int lenTxt = part[i].Length;
                // expected colorized text. (multicolor text in one cell)
                position.Characters[nLenTotal - lenTxt, lenTxt].Font.Color = myColor;
            }
            ((Microsoft.Office.Interop.Excel.Range)_sheet.Columns[1]).EntireRow.ColumnWidth = 15;

            saveExcel();
        }
        public void saveExcel()
        {
            string appPath = System.IO.Path.GetDirectoryName(
                Assembly.GetEntryAssembly().Location);
            string filename = Path.Combine(appPath, "MyDocument.xlsx");

            try
            {
                workbook.SaveAs(filename);
                workbook.Close();
            }
            catch (Exception)
            {
                workbook.Close();
            }
        }
    }
}

推荐答案

看看这里:< a href =http://stackoverflow.com/questions/9469682/is-it-possible-to-do-multiple-colored-text-within-an-excel-cell> http://stackoverflow.com/questions / 9469682 /它是可以做多色文本的一个excel-cell [ ^ ]。基本思路是使用 Cell.Cheracters(开始,长度).Font = yourFont
Have a look here: http://stackoverflow.com/questions/9469682/is-it-possible-to-do-multiple-colored-text-within-an-excel-cell[^]. The base idea is to use Cell.Cheracters(Start, Length).Font = yourFont


这篇关于如何使用C#在Excelcell中多色文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆