How to add header column in excel using Java Apache POI?

Madhusudan

I am writing a java program in which I have to take data from XML file and put it into excel file. While doing this I have to create row headers and add data accordingly.

I know how to create column headers. I can do it in following way:

....
HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet("EDR Raw Data");

    Row header = sheet.createRow(0);
    header.createCell(0).setCellValue("Header1");
    header.createCell(1).setCellValue("Header2");
    header.createCell(2).setCellValue("Header3");
    header.createCell(3).setCellValue("Header4");
....

It creates excel file as shown below: enter image description here

But I want my excel file looks like below:

enter image description here

I am not getting a way to do this. It's not good idea to create required number of rows individually. Is there any way by which we can create a column and add all header in that column?

The way I tried to do this is:

....
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("EDR Raw Data");

sheet.createRow(0).createCell(0).setCellValue("header1");
sheet.createRow(1).createCell(0).setCellValue("header2");
sheet.createRow(2).createCell(0).setCellValue("header3");
sheet.createRow(3).createCell(0).setCellValue("header4");
...

But in my case I have to give 100+ row labels. SO this won't be efficient way.

Gagravarr

Just create one row for each header, and populate the first cell in each!

Something like:

String[] headers = new String[] { "Header1", "Header2", "Header3" };

Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("EDR Raw Data");

for (int rn=0; rn<headers.length; rn++) {
   Row r = sheet.createRow(rn);
   r.createCell(0).setCellValue(headers[rn]);
}

Then when populating your data, do sheet.getRow(rownumber) to get the existing row, and populate the remaining cells of interest in it

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to add Data validation to entire column of an excel sheet using apache poi in java?

How to write column by column data in Excel using Apache POI?

How to put a table inside header of word using java apache poi?

how to get last column value of respective row of excel sheet in java using Apache POI

Using Apache POI how to read a specific excel column

How to add column field in pivottable using Apache POI

Lock single column in Excel using Apache POI

Insert image in column to excel using Apache POI

How to insert a table in ms excel using apache java poi

how to write metadata into Excel workbooks using Apache POI in Java

How to remove warning in Excel using apache poi in JAVA?

How to Convert PDF to Excel in java using Apache Poi

How to write in cell in excel using apache POI in JAVA?

How to add text to a .docx file using Apache POI in Java

Apache POI: How to insert column in Excel file

How to auto adjust the column in excel in apache POI

How to get number of rows in excel which contains data(excluding blank or empty cells) in one particular column using JAVA, APACHE POI

Exporting to Excel in Java using Apache POI

Insert a Row in Excel Using Java Apache POI

How to add superscript text using Apache POI

Filtering column in excel using java poi

Not able to check in excel using Apache POI if specific cell as per row and column is empty in Selenium with Java

Apache Poi - Java-: How to add text containing blank lines as separate paragraphs to a Word document using Apache POI?

How to add new sheets to existing excel workbook using apache POI and PrimeFaces

How can add different colour for each worksheet in excel using Apache POI

How to add columns to an existing large excel file using SXSSF Apache POI?

Apache POI Add Column Label

Update existing Excel file using Apache POI using java

How to check if excel column headers are in certain order in apache poi

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive