Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在excel C中为特定行着色#_C#_Excel - Fatal编程技术网

C# 在excel C中为特定行着色#

C# 在excel C中为特定行着色#,c#,excel,C#,Excel,我正在创建每日报告的excel工作簿。我想把标题部分涂成黄色。发布的所有链接要么需要打开工作簿的链接,要么不是特定于probelm的。我在这里发布我的代码,请建议如何将第6行设置为黄色 string workBookName; // creating Excel Application Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.

我正在创建每日报告的excel工作簿。我想把标题部分涂成黄色。发布的所有链接要么需要打开工作簿的链接,要么不是特定于probelm的。我在这里发布我的代码,请建议如何将第6行设置为黄色

    string workBookName;

        // creating Excel Application
        Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();


        // creating new WorkBook within Excel application
        Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);


        // creating new Excelsheet in workbook
        Microsoft.Office.Interop.Excel._Worksheet worksheet = null;

        // see the excel sheet behind the program
        app.Visible = true;

        // get the reference of first sheet. By default its name is Sheet1.
        // store its reference to worksheet
        worksheet = workbook.Sheets["Sheet1"];
        worksheet = workbook.ActiveSheet;

        // changing the name of active sheet
        workBookName = DateTime.Now.ToString("ddMMMyyyy-HHmmss");
        worksheet.Name = workBookName;

        worksheet.Cells[1, 1] = "Logistics";
        worksheet.Cells[2, 1] = "Tracking Number";

        worksheet.Cells[4, 2] = "Date - ";
        worksheet.Cells[4, 3] = dateTimePicker1.Value.ToString("dd/MMM/yyyy");


        // storing header part in Excel
        for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
        {
            worksheet.Cells[6, i] = dataGridView1.Columns[i - 1].HeaderText;
           // worksheet.get_Range(worksheet.Cells[6, i]).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Green);
        }



        // storing Each row and column value to excel sheet
        for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
        {
            for (int j = 0; j < dataGridView1.Columns.Count; j++)
            {
                //if (!string.IsNullOrWhiteSpace(dataGridView1.Rows[i].Cells[j].Value))
                {
                    worksheet.Cells[i + 8, j + 1] = dataGridView1.Rows[i].Cells[j].Value;


                }
            }
        }


        // save the application
        workbook.SaveAs("Tracking Number Report " + workBookName + ".xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

        // Exit from the application
        app.Quit();
字符串工作簿名;
//创建Excel应用程序
Microsoft.Office.Interop.Excel.\u应用程序app=新的Microsoft.Office.Interop.Excel.Application();
//在Excel应用程序中创建新工作簿
Microsoft.Office.Interop.Excel.\u工作簿=app.Workbooks.Add(Type.Missing);
//在工作簿中创建新的Excel工作表
Microsoft.Office.Interop.Excel.\u工作表=null;
//请参见程序后面的excel表
app.Visible=true;
//获取第一张图纸的参考。默认情况下,其名称为Sheet1。
//将其引用存储到工作表
工作表=工作簿。工作表[“Sheet1”];
工作表=工作簿.ActiveSheet;
//更改活动图纸的名称
workBookName=DateTime.Now.ToString(“ddMMMyyyy HHmmss”);
worksheet.Name=工作簿名;
工作表.单元格[1,1]=“物流”;
工作表.单元格[2,1]=“跟踪编号”;
工作表。单元格[4,2]=“日期-”;
worksheet.Cells[4,3]=dateTimePicker1.Value.ToString(“dd/MMM/yyyy”);
//在Excel中存储标题部分
对于(int i=1;i

我希望第6行(在我的例子中是标题部分)是彩色的。

如果运行此代码,您将看到一个Excel实例仍在运行,因为您尚未使用
调用在interop中释放资源。ReleaseComObject
调用-关于此的更多信息

我更喜欢使用一个开放的XML库,比如用于这种类型的东西。它使事情变得简单得多,并且很容易给一行上色

试着这样做:

using (var excel = new ExcelPackage())
{
    var workBookName = DateTime.Now.ToString("ddMMMyyyy-HHmmss");
    var worksheet = excel.Workbook.Worksheets.Add(workBookName);


    worksheet.Cells[1, 1].Value = "Logistics";
    worksheet.Cells[2, 1].Value = "Tracking Number";

    worksheet.Cells[4, 2].Value = "Date - ";
    worksheet.Cells[4, 3].Value = dateTimePicker1.Value.ToString("dd/MMM/yyyy");

    for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
    {
        worksheet.Cells[6, i].Value = dataGridView1.Columns[i - 1].HeaderText;
    }
    worksheet.Cells["6:6"].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    worksheet.Cells["6:6"].Style.Fill.BackgroundColor.SetColor(Color.Yellow);
    //
    //..etc
    //
    excel.SaveAs(new FileInfo("Tracking Number Report " + workBookName + ".xlsx"));
}
使用(var excel=new ExcelPackage())
{
var workBookName=DateTime.Now.ToString(“ddMMMyyyy HHmmss”);
var worksheet=excel.Workbook.Worksheets.Add(workBookName);
工作表.单元格[1,1].Value=“物流”;
工作表。单元格[2,1]。Value=“跟踪编号”;
工作表。单元格[4,2]。Value=“日期-”;
worksheet.Cells[4,3].Value=dateTimePicker1.Value.ToString(“dd/MMM/yyyy”);
对于(int i=1;i
如果您运行此代码,您将看到一个Excel实例仍在运行,因为您尚未使用
在interop中释放资源。ReleaseComObject
调用-有关此操作的更多信息

我更喜欢使用一个开放的XML库,比如用于这种类型的东西。它使事情变得简单得多,并且很容易给一行上色

试着这样做:

using (var excel = new ExcelPackage())
{
    var workBookName = DateTime.Now.ToString("ddMMMyyyy-HHmmss");
    var worksheet = excel.Workbook.Worksheets.Add(workBookName);


    worksheet.Cells[1, 1].Value = "Logistics";
    worksheet.Cells[2, 1].Value = "Tracking Number";

    worksheet.Cells[4, 2].Value = "Date - ";
    worksheet.Cells[4, 3].Value = dateTimePicker1.Value.ToString("dd/MMM/yyyy");

    for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
    {
        worksheet.Cells[6, i].Value = dataGridView1.Columns[i - 1].HeaderText;
    }
    worksheet.Cells["6:6"].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    worksheet.Cells["6:6"].Style.Fill.BackgroundColor.SetColor(Color.Yellow);
    //
    //..etc
    //
    excel.SaveAs(new FileInfo("Tracking Number Report " + workBookName + ".xlsx"));
}
使用(var excel=new ExcelPackage())
{
var workBookName=DateTime.Now.ToString(“ddMMMyyyy HHmmss”);
var worksheet=excel.Workbook.Worksheets.Add(workBookName);
工作表.单元格[1,1].Value=“物流”;
工作表。单元格[2,1]。Value=“跟踪编号”;
工作表。单元格[4,2]。Value=“日期-”;
worksheet.Cells[4,3].Value=dateTimePicker1.Value.ToString(“dd/MMM/yyyy”);
对于(int i=1;i
我引用了此链接,但这没有帮助,因为我正在创建excel,然后我想给行上色。在这种情况下,用户将从选定路径打开文件。我无法使用从路径选择的系统。可能重复的可能重复我引用了此链接,但没有帮助,因为在我的情况下,我正在创建excel,然后我想给行着色。在这种情况下,用户将从选定路径打开文件。我不能用那种从路径中选择的方法。可能是重复的,谢谢你的建议。。它工作了,而且非常轻和容易。谢谢你的建议。。它工作了,而且非常轻和容易。谢谢