【问题标题】:Reading specific columns in Excel Sheet读取 Excel 工作表中的特定列
【发布时间】:2017-03-30 11:45:18
【问题描述】:

我有一个 Excel 电子表格,但现在我的问题是我希望能够只读取特定的列,电子表格中的列超过 20,我只需要读取 3 列。

procedure TForm1.sh1(SheetIndex: integer);
Var
  Xlapp1,Xlrange, Sheet:Variant ;
  MaxRow, MaxCol,X, Y:integer ;
  str:string;
  arrData:Variant;
begin
 try
  Str:=trim(form1.OpenDialog1.FileName);

  XLApp1 := createoleobject('excel.application');
  XLApp1.Workbooks.open(Str) ;

  Sheet := XLApp1.WorkSheets[SheetIndex] ;

  MaxRow := Sheet.Usedrange.EntireRow.count ;
  MaxCol := sheet.Usedrange.EntireColumn.count;

  arrData:= Sheet.UsedRange.Value;

  stringgrid1.RowCount:=maxRow+1;
  StringGrid1.ColCount:=maxCol+1;

  for x := 1 to maxCol do
    for y := 1 to maxRow do
     stringgrid1.Cells[x,y]:=arrData[y, x];

  XLApp1.Workbooks.close;
 Except
  on E : Exception do begin
  XLApp1.Workbooks.close;
   ShowMessage(E.Message);
  end;
 end;
end;

【问题讨论】:

  • 是什么阻止您阅读您想阅读的栏目?
  • 这段代码读取每一行和每一列。你有什么尝试只阅读你需要的 3 列?在大多数任何语言中,这似乎都是一项简单的编程任务……出了什么问题?
  • 我在 StringGrid 中加载这些数据,我的挑战是我不想遍历每一列
  • 将你要读取的每一列定义为一个范围,一次性转移每个范围。
  • 我对你的 q 做了一个小小的修改,这样我就可以删除我之前的反对票,因为我在谷歌上搜索答案时重新发现了它。

标签: excel delphi delphi-xe3


【解决方案1】:

下面是一个从 Excel 电子表格中动态检索三个完整列(H、I 和 J)内容的示例。虽然它不是针对您的特定示例量身定制的,但它应该为您提供这样做的基本概念(并在之后正确清理),您可以适应您的特定需求。我已经对代码进行了注释以明确它在做什么。

procedure TForm1.Button1Click(Sender: TObject);
var
  Excel, Book, Sheet, Range1: OleVariant;
  i, j: Integer;
  Data: Variant;
const
  // Obtained at https://msdn.microsoft.com/en-us/library/office/ff820880.aspx
  xlDown = -4121;
begin
  Excel := CreateOleObject('Excel.Application');
  try
    Book := Excel.WorkBooks.Open('E:\TempFiles\Test.xlsx');
    Sheet := Book.Worksheets.Item['Sheet1'];

    // Get tne range we want to extract, in this case all rows of columns H-J.
    // .End(xlDown) finds the last used cell in the indicated column
    Range1 := Sheet.Range['H1', Sheet.Range['J1'].End[xlDown]];
    Data := Range1.Value;

    // Get the number of columns and rows from the array itself. The addition
    // of 1 is for the fixed row and column, and to synch up with the Data
    // array being 1 based instead of 0
    StringGrid1.ColCount := VarArrayHighBound(Data, 2) + 1;
    StringGrid1.RowCount := VarArrayHighBound(Data, 1) + 1;

    // Get the number of columns and rows from the array itself.
    // We know that what is being returned is a two dimensional array
    // (rows and columns).
    //
    // Add 1 to allow for the fixed row and column, and to synch up with the Data,
    // where the arrays are 1 based instead of 0
    //
    for i := 1 to StringGrid1.ColCount - 1 do
      for j := 1 to StringGrid1.RowCount - 1 do
        StringGrid1.Cells[i, j] := Data[j, i];

  finally
    // Clean up all references so Excel will close cleanly
    Range1 := Unassigned;
    Sheet := Unassigned;
    Book := Unassigned;
    Excel.Quit;
    Excel := Unassigned;
  end;
end;

【讨论】:

  • 谢谢,工作完美,正是我需要的,语言有时会成为障碍,但其他人不会看到。
  • 如果您想要 B、N 和 O 列而不是 H-J 范围,该怎么办?我试过这个但没有运气 Range1 := Sheet.Range['B1,N1,O1'];
  • @Fero68:那将是一个明显不同的问题。这个已经回答了。当然,一个稍微简单的解决方案是简单地使用三个单独的范围,因为它们不是连续的。有效范围可以小到一个单元格、一列、部分列或一行。
  • @KenWhite。太棒了!我只是想提醒自己如何在 Excel 中做类似的事情,这是谷歌的热门搜索。 +1
  • @MartynA:很高兴为您提供帮助。如果需要,可以在this answer中找到逆向操作(StringGrid转Excel)
猜你喜欢
  • 1970-01-01
  • 2018-07-26
  • 2021-02-09
  • 2020-12-26
  • 2022-12-04
  • 1970-01-01
  • 2019-05-15
  • 2021-12-24
  • 2010-11-14
相关资源
最近更新 更多