Bug 47862 - Not setting the correct columnwidth in XSSFSheet
Summary: Not setting the correct columnwidth in XSSFSheet
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: XSSF (show other bugs)
Version: 3.5-dev
Hardware: PC Windows XP
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-09-17 13:06 UTC by bob Stox
Modified: 2009-10-04 03:15 UTC (History)
0 users



Attachments
Preformatted spreadsheet (8.21 KB, application/octet-stream)
2009-09-17 13:06 UTC, bob Stox
Details

Note You need to log in before you can comment on or make changes to this bug.
Description bob Stox 2009-09-17 13:06:04 UTC
Created attachment 24284 [details]
Preformatted spreadsheet

I have a spreadsheet that is already preformatted. If you have consecutive
columns with the same column width and you want to change just one of those
column's width.  When you do it changes all the columns to that width.

I will attach a spreadsheet and a test program to illustrate the problem.

import java.io.*;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import java.util.*;
public class Width_test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {

        Workbook wb = new XSSFWorkbook("checkwidth3.xlsx");
        Sheet sheet = wb.getSheetAt(0);

        for (int i=5; i > -1;i--){
            System.out.println("Column: " + i + " width: " +
sheet.getColumnWidth(i));
            sheet.setColumnWidth(i, sheet.getColumnWidth(i)-1000);
            System.out.println("Column: " + i + " width: " +
sheet.getColumnWidth(i));
        }

        FileOutputStream fileOut = new FileOutputStream("checkwidth2.xlsx");
        wb.write(fileOut);
        fileOut.close();
        } catch (Exception e) {
        }
    }
}
Comment 1 David Fisher 2009-09-17 14:14:37 UTC
Are you running in the latest nightly build?

Yegor just resolved a similar XSSF issue in https://issues.apache.org/bugzilla/show_bug.cgi?id=47581

Regards,
Dave
Comment 2 bob Stox 2009-09-17 18:31:08 UTC
(In reply to comment #1)
> Are you running in the latest nightly build?
> Yegor just resolved a similar XSSF issue in
> https://issues.apache.org/bugzilla/show_bug.cgi?id=47581
> Regards,
> Dave

I tried and it still is working the same way

Bob
Comment 3 bob Stox 2009-09-22 07:29:43 UTC
After looking at what Yegor did for bug 47581, I was debugging the setColumnWidth in class ColumnHelper.  I add the following line:
System.out.println(col.toString);

And the result was the following:
<xml-fragment min="1" max="5" customWidth="true" width="15.77734375" collapsed="false" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"/>

I saw the min, max to be the columns that have the same width in my spreadsheet.

I first tried just setting the min/max for that column and that worked for the one column but all the other columns were reset to the default width. So I then came up with the following solution and it seems to work.

    public void setColWidth(long index, double width) {
        CTCol col = getOrCreateColumn1Based(index+1, false);
        if (col.getMax() != col.getMin()){
        	double old_width = col.getWidth();
        	long from_column = col.getMin();
        	long to_column = col.getMax();
            col.setMin(index+1);
            col.setMax(index+1);	
            // Check if there are any columns to the left of columns to be reset
            if (index >= from_column && index > 0){
            	CTCol newCol = getOrCreateColumn1Based(index, false);
            	newCol.setMin(from_column);
            	newCol.setMax(index);
            	setColumnAttributes(col,newCol);
            	newCol.setWidth(old_width);
            }
            // Check if there are any columns to the left of columns to be reset
            if ((index+2) <= to_column){
            	CTCol newCol = getOrCreateColumn1Based((index+2), false);
            	newCol.setMin(index+2);
            	newCol.setMax(to_column);
            	setColumnAttributes(col,newCol);
            	newCol.setWidth(old_width);
            }
        }
        col.setWidth(width);
    }
Comment 4 Yegor Kozlov 2009-10-04 03:15:32 UTC
Actually the fix is simple - change the second parameter of getOrCreateColumn1Based from false to true in ColumnHelper.setColWidth:

    public void setColWidth(long index, double width) {
        CTCol col = getOrCreateColumn1Based(index+1, true);
        col.setWidth(width);
    }

true causes to split spanned columns which is exactly what we need.


The fix was committed in r821497

Yegor