在 Java 中访问 Excel 文件的列标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24430222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:09:10  来源:igfitidea点击:

Accessing column headers of an excel file in Java

javaexcel

提问by Abhishek

I want to access data related to headers in my excel file . For example, as shown in attached image , 724 is related to ValueSetId and RediffZone is related to ProductName.

我想访问与我的 excel 文件中的标题相关的数据。例如,如附图所示,724 与 ValueSetId 相关,RediffZone 与 ProductName 相关。

I want some operations which will be based on headers, for example , for all the users with sampleName Cat and ValuseSetId 724 ,the valueSetID should be incremented by a random number .

我想要一些基于标题的操作,例如,对于所有具有 sampleName Cat 和 ValuseSetId 724 的用户,valueSetID 应该增加一个随机数。

And I have made headers in excel file using insert table menu and setting headers. I think that is the only way to specify headers in excel for your columns .

我已经使用插入表格菜单和设置标题在 excel 文件中制作了标题。我认为这是在 excel 中为您的列指定标题的唯一方法。

My excel is like this:( i couldn't attach image as I don't have enough point,sorry abt that !!)

我的 excel 是这样的:(我无法附加图片,因为我没有足够的观点,对不起!!)

ProductName      SampleName      Categoryname         ValueSetID
----------------------------------------------------------------------------
Sherrif  /           She /            Primal  /            256
----------------------------------------------------------------------------
CateZore /           Cat /            Non-Primal /          724
------------------------------------------------------------------------------
RediffZone /          Red /            Primal /             991
-------------------------------------------------------------------------------

And Header header=sheet.getHeader(); don't give those headers abt which I am talking .

和 Header header=sheet.getHeader(); 不要给我正在谈论的那些标题 abt。

回答by Jamie Cockburn

getHeader()has nothing to do with the column headings, that function is to do with page headings when the document is printed.

getHeader()与列标题无关,该功能与打印文档时的页面标题有关。

Excel, and therefore POI have no concept of the columns themselves having headers. You just need to access the data in the first row of the sheet:

Excel,因此 POI 没有列本身具有标题的概念。您只需要访问工作表第一行中的数据:

// Assuming "column headers" are in the first row
XSSFRow header_row = sheet.getRow(0);

// Assuming 4 columns
for (int i = 0; i < 4; i++) {
     XSSFCell header_cell = header_row.getCell(i);
     String header = header_cell = header_cell.getStringCellValue();
     // Do something with string
}

If you wanted a variable number of columns, you could instead loop until the cell value is blank.

如果您想要可变数量的列,您可以改为循环,直到单元格值为空。

回答by Bentaye

I think you can't tell if the cell is a Header or not. However you can get its style using getCellStyle()

我认为您无法判断该单元格是否为 Header。但是,您可以使用getCellStyle()获取其样式

Then you might be able to say if it is a header based on the font style/size/color etc ..

然后你可以说它是否是基于字体样式/大小/颜色等的标题。

Here is example to check if the first cell's style is bold:

这是检查第一个单元格的样式是否为粗体的示例:

XSSFSheet sheet = workbook.getSheetAt(0);
XSSFRow row = sheet.getRow(0);
XSSFCell cell = row.getCell(0);
XSSFCellStyle style = cell.getCellStyle();
boolean isBold = style.getFont().getBold();

System.out.println("isBold " + isBold);