在当前上下文中不存在c#

时间:2014-03-22 02:11:15

标签: c# visual-studio-2013

从销售组件以便能够使用excel文件的网站的示例部分获取此信息

我将它复制粘贴到visual studio 2013中,我不断收到此错误...

当前上下文中不存在“颜色”...

我一直在网上阅读这些错误,但有些人提供了解决方案,但没有 关于“为什么”这个错误存在的关键答案

关于在方法内部或外部声明变量的事情,就像在这个网站上许多人一样冒险尝试newb的路径

感谢您的帮助

这里的链接是你感兴趣的

http://www.gemboxsoftware.com/SampleExplorer/Spreadsheet/BasicFeatures/StylesandFormatting?tab=cs

using System;
using System.Drawing;
using System.IO;
using GemBox.Spreadsheet;

class Sample
{
[STAThread]
static void Main(string[] args)
{
    // If using Professional version, put your serial key below.
    SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

    ExcelFile ef = new ExcelFile();
    ExcelWorksheet ws = ef.Worksheets.Add("Styles and Formatting");

    ws.Cells[0, 1].Value = "Cell style examples:";
    ws.PrintOptions.PrintGridlines = true;

    int row = 0;

    // Column width of 4, 30 and 35 characters.
    ws.Columns[0].Width = 4 * 256;
    ws.Columns[1].Width = 30 * 256;
    ws.Columns[2].Width = 35 * 256;

    ws.Cells[row += 2, 1].Value = ".Style.Borders.SetBorders(...)";
    ws.Cells[row, 2].Style.Borders.SetBorders(MultipleBorders.All, Color.FromArgb(252, 1, 1), LineStyle.Thin);

    ws.Cells[row += 2, 1].Value = ".Style.FillPattern.SetPattern(...)";
    ws.Cells[row, 2].Style.FillPattern.SetPattern(FillPatternStyle.ThinHorizontalCrosshatch, Color.Green, Color.Yellow);

    ws.Cells[row += 2, 1].Value = ".Style.Font.Color =";
    ws.Cells[row, 2].Value = "Color.Blue";
    ws.Cells[row, 2].Style.Font.Color = Color.Blue;

    ws.Cells[row += 2, 1].Value = ".Style.Font.Italic =";
    ws.Cells[row, 2].Value = "true";
    ws.Cells[row, 2].Style.Font.Italic = true;

    ws.Cells[row += 2, 1].Value = ".Style.Font.Name =";
    ws.Cells[row, 2].Value = "Comic Sans MS";
    ws.Cells[row, 2].Style.Font.Name = "Comic Sans MS";

    ws.Cells[row += 2, 1].Value = ".Style.Font.ScriptPosition =";
    ws.Cells[row, 2].Value = "ScriptPosition.Superscript";
    ws.Cells[row, 2].Style.Font.ScriptPosition = ScriptPosition.Superscript;

    ws.Cells[row += 2, 1].Value = ".Style.Font.Size =";
    ws.Cells[row, 2].Value = "18 * 20";
    ws.Cells[row, 2].Style.Font.Size = 18 * 20;

    ws.Cells[row += 2, 1].Value = ".Style.Font.Strikeout =";
    ws.Cells[row, 2].Value = "true";
    ws.Cells[row, 2].Style.Font.Strikeout = true;

    ws.Cells[row += 2, 1].Value = ".Style.Font.UnderlineStyle =";
    ws.Cells[row, 2].Value = "UnderlineStyle.Double";
    ws.Cells[row, 2].Style.Font.UnderlineStyle = UnderlineStyle.Double;

    ws.Cells[row += 2, 1].Value = ".Style.Font.Weight =";
    ws.Cells[row, 2].Value = "ExcelFont.BoldWeight";
    ws.Cells[row, 2].Style.Font.Weight = ExcelFont.BoldWeight;

    ws.Cells[row += 2, 1].Value = ".Style.HorizontalAlignment =";
    ws.Cells[row, 2].Value = "HorizontalAlignmentStyle.Center";
    ws.Cells[row, 2].Style.HorizontalAlignment = HorizontalAlignmentStyle.Center;

    ws.Cells[row += 2, 1].Value = ".Style.Indent";
    ws.Cells[row, 2].Value = "five";
    ws.Cells[row, 2].Style.HorizontalAlignment = HorizontalAlignmentStyle.Left;
    ws.Cells[row, 2].Style.Indent = 5;

    ws.Cells[row += 2, 1].Value = ".Style.IsTextVertical = ";
    ws.Cells[row, 2].Value = "true";
    // Set row height to 50 points.
    ws.Rows[row].Height = 50 * 20;
    ws.Cells[row, 2].Style.IsTextVertical = true;

    ws.Cells[row += 2, 1].Value = ".Style.NumberFormat";
    ws.Cells[row, 2].Value = 1234;
    ws.Cells[row, 2].Style.NumberFormat = "#.##0,00 [$Krakozhian Money Units]";

    ws.Cells[row += 2, 1].Value = ".Style.Rotation";
    ws.Cells[row, 2].Value = "35 degrees up";
    ws.Cells[row, 2].Style.Rotation = 35;

    ws.Cells[row += 2, 1].Value = ".Style.ShrinkToFit";
    ws.Cells[row, 2].Value = "This property is set to true so this text appears shrunk.";
    ws.Cells[row, 2].Style.ShrinkToFit = true;

    ws.Cells[row += 2, 1].Value = ".Style.VerticalAlignment =";
    ws.Cells[row, 2].Value = "VerticalAlignmentStyle.Top";
    // Set row height to 30 points.
    ws.Rows[row].Height = 30 * 20;
    ws.Cells[row, 2].Style.VerticalAlignment = VerticalAlignmentStyle.Top;

    ws.Cells[row += 2, 1].Value = ".Style.WrapText";
    ws.Cells[row, 2].Value = "This property is set to true so this text appears broken   into multiple lines.";
    ws.Cells[row, 2].Style.WrapText = true;

    ef.Save("Styles and Formatting.xls");
 }
}

3 个答案:

答案 0 :(得分:3)

确保在解决方案资源管理器中的引用下引用System.Drawing dll。

MSDN中记录了任何.NET类,每次您都会看到它所在的命名空间和程序集。

必须在References的{​​{1}}下添加大会,并且必须使用Solution Explorer指令将命名空间添加到您的代码中。

例如,这是MSDN中Color类的快照:

enter image description here

http://msdn.microsoft.com/en-us/library/system.drawing.color(v=vs.110).aspx

干杯

答案 1 :(得分:2)

Color位于System.Drawing程序集中的System.Drawing.dll命名空间中。因此,除了使用声明:

using System.Drawing;

您需要添加对System.Drawing程序集的引用。默认情况下,它不会在控制台项目模板中引用。

答案 2 :(得分:0)

请阅读this。 :)

也许您正在复制示例,但没有引用GemBox.Spreadsheet。 如果您想使用this,则应将其添加为参考。