获取单元格范围Excel C ++ / cli

时间:2013-07-09 21:08:17

标签: .net excel c++-cli excel-interop

我有一个Excel电子表格,我想从.Net框架中提取数据。工作表在某种程度上看起来像这样:

      A     B    C    D    E    F    G
1  |hello| by | it |____|____| it | is |
2  | Hi  | he | To |____|____|you | are|

如何将两个“表”分成两个不同的数据表?

我明白有

Excel::Range^ RANGE = Worksheet->Cells;

但对于一个我不明白如何利用Range。两个,阅读后显然返回工作表的整个范围,并没有分开两个“表”。

有谁知道如何获得范围“A1:C2”和F1:G2“?

3 个答案:

答案 0 :(得分:0)

Worksheet->Cells应该是整个电子表格。你要求它代表整个工作表中所有单元格的Range^,这就是你得到的。 (引用MSDN:“返回一个Range对象,它表示工作表上的所有单元格(而不仅仅是当前正在使用的单元格)。”)

Range对象只代表一组单元格;它与工作表中的子表无关,例如“A1:C2”和“F1:G2”。

要获取子表,请使用CurrentRegion属性。我相信这样做的方法是获取单元格A1并调用CurrentRegion以获得A1:C2,并在F1上获得F1:G2。

(显然,如果可能的话,更好的设计是在工作簿中将两个子表放在自己的工作表中。)

答案 1 :(得分:0)

//the namespace you'll need to include, Project->Properties->Add New Reference
using namespace Microsoft::Office::Interop::Excel;

//This is your particular Excel instance
Excel::Application^ xl = gcnew Excel::Application();
//I like being able to see the sheet for simple programs
xl->Visible = true;

Excel::Workbook^ wb = xl->Workbooks->Open(Path2ExcelFile, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing);
Excel::Worksheet^ ws1 = static_cast<Excel::Worksheet^>(wb->Sheets[SheetName1]);
Excel::Worksheet^ ws2 = static_cast<Excel::Worksheet^>(wb->Sheets[SheetName2]);

现在我们可以获得范围

Range^ rng1 = ws->Range["A1:C2","A1:C2"];
Range^ rng1 = ws->Range["F1:G2","F1:G2"];

以下是如何获取单个细胞的值

String^ val1 = ws->Range["A1","A1"]->Value2->ToString();

然后另一种获得单个细胞价值的方法

ws->Range[((Excel::Range^)ws->Cells[(System::Object^)rowInteger, (System::Object^)columnInteger]), ((Excel::Range^)ws->Cells[(System::Object^)rowInteger, (System::Object^)columnInteger])]->Value2->ToString();

答案 2 :(得分:-1)

Microsoft :: Office :: Interop :: Excel :: Application ^ xl = gcnew Microsoft :: Office :: Interop :: Excel :: ApplicationClass();

Microsoft :: Office :: Interop :: Excel :: Workbook ^ wb = xl-> Workbooks-> Open(path,Type :: Missing,Type :: Missing,Type :: Missing,Type :: Missing,Type ::: Missing,Type :: Missing,Type :: Missing,Type :: Missing,Type :: Missing,Type :: Missing,Type :: Missing,Type :: Missing,Type :: Missing,Type :: Missing); Microsoft :: Office :: Interop :: Excel :: Worksheet ^ ws = static_castMicrosoft :: Office :: Interop :: Excel :: Worksheet ^(wb-> Sheets [“ Tabelle1”]);

            Microsoft::Office::Interop::Excel::Range^ rng1 = ws->UsedRange;
            // ----------------------------------------------------------------------


            for (int Rnum = 2; Rnum <= rng1->Rows->Count; Rnum++)
            {
                DataRow^ dr = dt->NewRow();
                for (int Cnum = 1; Cnum <= rng1->Columns->Count; Cnum++)
                {
                    if (((Microsoft::Office::Interop::Excel::Range^)rng1->Cells[Rnum, Cnum])->Value2 != nullptr)
                    {
                        dr[Cnum - 1] = ((Microsoft::Office::Interop::Excel::Range^)rng1->Cells[Rnum, Cnum])->Value2->ToString();
                    }
                }
                dt->Rows->Add(dr);
                dt->AcceptChanges();
            }


            
            for each (DataRow ^ dr in dt->Rows)
            {
                _dataGridView_main->Rows->Add(dr->ItemArray);
            }

            // Close Excel file after reading
            xl->Quit();