0

我有一个 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“的范围?

4

3 回答 3

0

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

一个Range对象只是代表一组细胞;它与工作表中的子表并不天生相关,就像您的“A1:C2”和“F1:G2”一样。

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

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

于 2013-07-09T21:50:32.780 回答
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();
于 2017-01-10T15:30:51.520 回答
-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 , 类型::缺失, 类型::缺失, 类型::缺失, 类型::缺失, 类型::缺失, 类型::缺失, 类型::缺失, 类型::缺失, 类型::缺失); 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();
于 2020-08-06T04:48:02.530 回答