简体   繁体   English

在C#中使用文本颜色将数据从excel导出到数据表

[英]export data from excel to datatable with text color in C#

I'm trying to export the data from MS Excel sheet to a datatable using C# in VS 2010 WinForms application. 我正在尝试使用VS 2010 WinForms应用程序中的C#将MS Excel工作表中的数据导出到数据表中。

I want some rows to be eliminated from being exported in Excel based on the text forecolor, like the text color is in Black, Green and Red in color. 我希望从基于文本前色的Excel中导出一些行,例如将文本颜色设置为黑色,绿色和红色。 If the text color is Green in color, then I want to exclude that row from being exported. 如果文本颜色为绿色,那么我想从导出中排除该行。

Kindly let me know how to achieve this. 请让我知道如何实现这一目标。

You should iterate through rows in Excel and check the text color before exporting. 在导出之前,您应该遍历Excel中的行并检查文本颜色。 Open Excel file first. 首先打开Excel文件。

            Microsoft.Office.Interop.Excel.Application App = new Microsoft.Office.Interop.Excel.Application();
            App.Visible = false;
            App.WindowState = Excel.XlWindowState.xlMinimized;
            App.ShowStartupDialog = false;

            Excel.Workbook WorkBook = App.Workbooks.Open(File, 0, false, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

It is good to get the area that is in use first: 最好先获得正在使用的区域:

Excel.Range Area = worksheet.get_Range("A1", worksheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing));

Iterate: 重复:

                for (int R = 1; R <= Area.Rows.Count; R++)
                {
                Excel.Range Row = ((Excel.Range)Area[R, "A"]);
                if(Row.Font.Color != Excel.XlRgbColor.rgbGreen)
                {
                   // Get data from the cells and include into a
                   // collection of items that represents data to be exported.
                }
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM