Solved

Some Issues in  POI-HSSF -[ Java API To Access Microsoft Excel Format Files ]

Posted on 2004-08-13
6
3,471 Views
Last Modified: 2008-01-09
hi Experts,
              Has Anybody used HSSF of  the POI Project's [Provided by apache.org] , I got the binaries from the site

http://jakarta.apache.org/poi/hssf/

HSSF is the POI Project's pure Java implementation of the Excel '97(-2002) file format. HSSF provides a way to read spreadsheets create, modify, read and write XLS spreadsheets.

              With that i am able to create Simple Excel Files thru Java. But i dont know how to set the Sheet Properties.
That is the Excel that i am creating is unformatted. I deally i need to set these properties in my excel Files

1) Wrap Text to Cell Width
2) Set the COlumn Widths.. .. If anyone has previously solved the same problems can y ou please update me on the same..

Thanx
NewBieWebProgrammer..





0
Comment
Question by:FearFactor_x
  • 3
  • 2
6 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 20 total points
ID: 11791172
It would make it easier if they produced proper online javadoc ;-) Not sure about sheet properties, but this looks reasonably promising for cell widths:

http://jakarta.apache.org/poi/hssf/quick-guide.html
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 11791364
Column widths are set for all columns (of course) and the text wrap is set in the style that is applied to cells.  A note of caution on using the styles, I found it to best to create some global styles for reuse since the more styles you create the larger the file will be, so reuse your styles.  

               HSSFSheet sheet1 = wb.createSheet("Test Results");
                  sheet1.setColumnWidth((short)0, (short)2300);
                  sheet1.setColumnWidth((short)1, (short)50);
                  sheet1.setColumnWidth((short)2, (short)9500);
                  sheet1.setColumnWidth((short)3, (short)100);
                  sheet1.setColumnWidth((short)4, (short)2300);

HSSFPrintSetup printSet = sheet1.getPrintSetup();
                  printSet.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);
                  printSet.setLandscape(true);
                  printSet.setFitWidth((short)1);
                  printSet.setFitHeight((short)0);
                  sheet1.setFitToPage(true);
                  sheet1.setHorizontallyCenter(true);
                  HSSFHeader header = sheet1.getHeader();
                  header.setLeft(HSSFHeader.file()+" Automated Test Summary");
                  header.setRight(HSSFHeader.date()+"  "+HSSFHeader.time());
                  HSSFFooter footer = sheet1.getFooter();
                  footer.setRight( "Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages() );
                  wb.setRepeatingRowsAndColumns(0, 0,9,0,1);
                  sheet1.setDisplayGridlines(false);
                  HSSFRow bTitle = sheet1.createRow((short)0);
                  HSSFCell cTcell = bTitle.createCell((short)0);
                  cTcell.setCellValue("Automated Test Summary");
                  HSSFCellStyle st1 = WorkbookStyles.createStyle(wb, Status.NORMAL);
                  st1.setBorderBottom(HSSFCellStyle.BORDER_NONE);
                  st1.setBorderLeft(HSSFCellStyle.BORDER_NONE);
                  st1.setBorderRight(HSSFCellStyle.BORDER_NONE);
                  st1.setBorderTop(HSSFCellStyle.BORDER_NONE);
                  st1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
                  st1.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
                  st1.setFillBackgroundColor(HSSFColor.LIGHT_YELLOW.index);
                        st1.setWrapText(true);




                        cTcell.setCellStyle(st1);


I would say to go through the documentation and examples.  It is a bit different than javadocs but it does show how.
0
 

Author Comment

by:FearFactor_x
ID: 11791476
hi Experts,
                i used the setColumnWidth::HSSFSheet function to set the column Widths.. But
If i do so i am not able to write into those cells using row.createCell() Function.. But if i dont set the column widths i  am able to write..  {by the Term "i am not able to write.." i mean = The text that i write does not appear in the Excel file..}   Here is my complete SOuce code..


public void excelTester() {

String[][] Data={{"Row1: Column 1","Row 1: Column 2 "},{"Row2 : Column1","Row 2: COlumn 2"}};

int rowOffset=3;      // leave the first first 3 rows blank and start from the 4th one..
int columnOffset=1; // leave the first column blank and start from the next one..
int i=0,j=0;


HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
    sheet.setColumnWidth((short)1,(short)20); // Setting the width for column 1 as 20 as it going to hold a big string
   
for(i=0;i<=Data.length-1;i++){

         // Create a Row Here.
         HSSFRow row = sheet.createRow(rowOffset++);
   
          for(j=0;j<=Data[i].length-1;j++){
          row.createCell((short)(columnOffset+j)).setCellValue(Data[i][j]);
                            
                               
           }
                            
                      
    }

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();

}


FOr the above program when i run and open workbook.xls i get only the items in COLUMN displayed that is

           <blank> Row 1: Column 2
           <blank> Row 2: Column 2


The column 1 items are missing.. HOw do i overcome this problem..


NewBieProgrammer..









               HSSFSheet sheet1 = wb.createSheet("Test Results");
               sheet1.setColumnWidth((short)0, (short)2300);



                row.createCell((short)(columnOffset+j)).setCellValue(Data[i][j]);
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 7

Accepted Solution

by:
tomboshell earned 80 total points
ID: 11791544
I don't really see anything majorly wrong.  I think the only problem would be in the column width size.  POI sets the column width quite fine.  
try this:
sheet.setColumnWidth((short)1,(short)5700); // Setting the width for column 1 as 20 as it going to hold a big string
            sheet.setColumnWidth((short)2,(short)5700);

from the javadocs: "set the width (in units of 1/256th of a character width)"

This means that you actually have to use quite large numbers to view anything.
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 11791642
Thanks!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11791665
8-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
copyEndy  challenge 15 58
Unexpected HttpURLConnection connection behavior 2 52
recursion example 16 71
mockito example issue 8 38
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now