AllExperts > Java 
Search      
Java
Volunteer
Answers to thousands of questions
 Home · More Java Questions · Answer Library  · Encyclopedia ·
More Java Answers
Question Library

Ask a question about Java
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About 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

 
   

You are here:  Experts > Computing/Technology > Focus on Java > Java > Solution for a Java problem

Java - Solution for a Java problem


Expert: Brian Ngure - 9/10/2009

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

Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.