Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have a data table and I wrote them to excel file using JExcel API,

but my problem is I don't knowm how to set the width for each column in excel.

So I hope that you will help me to resolve this.

Thank in advance.
Posted
Comments
april88t 2-Mar-13 1:54am    
read this--- http://bethecoder.com/applications/tutorials/excel/jexcel-api/how-to-set-excel-column-width.html
i think this could help you

1 solution

Java
import java.io.File;
package com.bethecoder.tutorials.jexcelapi.write;
import java.io.IOException;
import jxl.Workbook;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class CellWidthTest {

  /**
   * @param args
   * @throws IOException 
   * @throws IOException 
   * @throws WriteException 
   * @throws BiffException 
   */
  public static void main(String[] args) throws IOException, WriteException {
    //Creates a writable workbook with the given file name
    WritableWorkbook workbook = Workbook.createWorkbook(new File("C:/JXL/CellWidth.xls"));
    WritableSheet sheet = workbook.createSheet("My Sheet", 0);
    
    // Create cell font and format
    WritableFont cellFont = new WritableFont(WritableFont.TIMES, 12);
    cellFont.setColour(Colour.BLUE);
    
    WritableCellFormat cellFormat = new WritableCellFormat(cellFont);
    cellFormat.setBackground(Colour.ORANGE);
    cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
    
    //Set cell width in CHARS
    int col = 2;
    int widthInChars = 3;
    sheet.setColumnView(col, widthInChars);
    sheet.addCell(new Label(col, 1, "A", cellFormat));    
    
    col = 3;
    widthInChars = 4;
    sheet.setColumnView(col, widthInChars);
    sheet.addCell(new Label(col, 1, "BB", cellFormat));  
    
    col = 4;
    widthInChars = 16;
    sheet.setColumnView(col, widthInChars);
    sheet.addCell(new Label(col, 1, "CCCCC", cellFormat));  
    
    //Writes out the data held in this workbook in Excel format
    workbook.write(); 
    
    //Close and free allocated memory 
    workbook.close(); 
  }

}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900