You are here:

Java/Solution for a Java problem

Advertisement


Question
Sir,
  I have read an excel file using java.I stored all the cell contents in a string array.One string contains one cell contents.
Now,I have a problem while writing on to a new Excel file.
  I am using Jakarta POI Api.How to create a cell array for a particular row using HSSFRow.

My code is like this:

try
     {
          HSSFWorkbook wb = new HSSFWorkbook();          // Create a New XL Document
          HSSFSheet sheet = wb.createSheet();            // Make a worksheet in the XL document created
          HSSFRow row = sheet.createRow((short) 0);      // Create row at index zero ( Top Row)
          for(int i=0;i<2;i++)
     {
          HSSFCell cell[] = new HSSFCell[2];
   HSSFCell cell = row.createCell((short) i);     
        //Create a cell at index zero ( Top Left)
          cell.setCellType(HSSFCell.CELL_TYPE_STRING);   
        // Lets make the cell a string type
              cell.setCellValue(1234567.0);                  
       // Type some content
           }
       
          // The Output file is where the xls will be created
        FileOutputStream fOut = new FileOutputStream(outputFile);
                // Write the XL sheet
                wb.write(fOut);
                fOut.flush();
                // Done Deal..
                fOut.close();
               System.out.println("File Created ..");

        }
        catch (Exception e)
      {
           System.out.println("!!BANG!! xlCreate() : " + e);
        }

So let me know how to move to next cell in same row using array
in HSSFRow.Or let me know how to create an object array for HSSFRow & HSSFCell.

      Thanking You.
With regards,
Muthu Pillai.

Answer
Hi,

I'm not sure exactly what the problem is. To move to the next cell, you just need to call row.createCell(int i); and the specify the column number i which is what you are doing here

for(int i = 0; i < 2; i++) {
   HSSFCell cell[] = new HSSFCell[2];

   //Create a cell at index zero ( Top Left)
   HSSFCell cell = row.createCell(i);

   // Lets make the cell a string type
   cell.setCellType(HSSFCell.CELL_TYPE_STRING);

   // Type some content
   cell.setCellValue(1234567.0);
}

As i increases, you should be moving along the row.


Brian

Java

All Answers


Answers by Expert:


Ask Experts

Volunteer


Brian Ngure

Expertise

Strong in Java. Will be able to answer both in areas of designing and coding. Some knowledge in servlets

Experience

5 years of Java experience, Sun Certified Java Associate and Sun Certified Java Programmer, JDK 1.4

©2012 About.com, a part of The New York Times Company. All rights reserved.