Criei esta classe com o código necessário para simular o que eu preciso. A necessidade é gerar um arquivo Excel com alguns dados e, que a célula ( tamanho da céula) seja o suficiente para acomodar o texto. Na maioria dos posts encontrei a indicação para usar:
sheet.autoSizeColumn(0)
Também
CellStyle style = cell.getCellStyle()
style.setWrapText(true)
cell.setCellStyle(style)
mas o setWrapText
faz a quebra de texto automaticamente, e não é o que eu queria.
Não consegui o resultado desejado. Gostaria de indicações do que posso fazer para resolver.
Update: Usando este trecho de código:
// style.setWrapText(true);
Cell cellSer1 = row.createCell(0);
// cellSer1 = row.createCell(0);
cellSer1.setCellStyle(style);
String myCellSer1 = "Nota Fiscal";
String text = myCellSer1;
int width = text.length();
sheet.setColumnWidth(0, width * 250);
String stringCellValueSer1 = myCellSer1;
cellSer1.setCellValue(stringCellValueSer1);
Consegui algo, já posso alterar o tamanho da coluna, porém é trabalhoso,e creio não ser uma boa solução.
Update2:
Exception in thread "main" java.lang.IllegalStateException: This worksheet does not contain merged regions at org.apache.poi.xssf.usermodel.XSSFSheet.getMergedRegions(XSSFSheet.java:1116) at org.apache.poi.ss.util.SheetUtil.getCellWidth(SheetUtil.java:110) at org.apache.poi.ss.util.SheetUtil.getColumnWidthForRow(SheetUtil.java:282) at org.apache.poi.ss.util.SheetUtil.getColumnWidth(SheetUtil.java:235) at org.apache.poi.ss.util.SheetUtil.getColumnWidth(SheetUtil.java:214) at org.apache.poi.xssf.usermodel.XSSFSheet.autoSizeColumn(XSSFSheet.java:400) at org.apache.poi.xssf.usermodel.XSSFSheet.autoSizeColumn(XSSFSheet.java:382) at view.XLSTest.main(XLSTest.java:98)
Update3:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class XLSTest {
public static void main(String[] args) throws FileNotFoundException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("sheetName");
File file;
file = new File("C:\\Temp\\Notas.xls");
FileOutputStream fos = new FileOutputStream(file);
HSSFRow row;
int count = 0;
String posicaoExcel = null;
row = sheet.createRow((short)count);
Cell cellSer1 = row.createCell(0);
String myCellSer1 = "Nota Fiscal";
String stringCellValueSer1 = myCellSer1;
cellSer1.setCellValue(stringCellValueSer1);
Cell cellnf1 = row.createCell(1);
String myCell1 = "Série";
String stringCellValue1 = myCell1;
cellnf1.setCellValue(stringCellValue1);
for (int i = 0; i <= 5; i++) {
count++;
posicaoExcel = "longgggggggggggggggggggggggggggg";
row = sheet.createRow((short)count);
Cell cellSer = row.createCell(0);
String myCellSer = posicaoExcel;
String stringCellValueSer = myCellSer;
cellSer.setCellValue(stringCellValueSer);
Cell cellnf = row.createCell(1);
String myCell = posicaoExcel;
String stringCellValue = myCell;
cellnf.setCellValue(stringCellValue);
}
int quantidadeColunas = sheet.getRow(0).getPhysicalNumberOfCells();
for(int i = 0; i < quantidadeColunas; i++ ) {
sheet.autoSizeColumn(i);
}
try {
wb.write(fos);
} catch (IOException ex) {
Logger.getLogger(XLSTest.class.getName()).log(Level.SEVERE, null, ex);
}
try {
fos.flush();
} catch (IOException ex) {
Logger.getLogger(XLSTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class XLSTest {
public static void main(String[] args) throws FileNotFoundException {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("sheetName");
File file;
file = new File("C:\\Users\\Lucio\\Documents\\Notas.xls");
FileOutputStream fos = new FileOutputStream(file);
XSSFRow row;
int count = 0;
String posicaoExcel = null;
XSSFCellStyle style = (XSSFCellStyle) wb.createCellStyle();
style.setBorderBottom(XSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
row = sheet.createRow(count);
Cell cellSer1 = row.createCell(0);
// cellSer1 = row.createCell(0);
cellSer1.setCellStyle(style);
String myCellSer1 = "Nota Fiscal";
String stringCellValueSer1 = myCellSer1;
cellSer1.setCellValue(stringCellValueSer1);
// sheet.autoSizeColumn(1);
Cell cellnf1 = row.createCell(1);
// cellnf1 = row.createCell(1);
String myCell1 = "Série";
cellnf1.setCellStyle(style);
String stringCellValue1 = myCell1;
cellnf1.setCellValue(stringCellValue1);
//sheet.autoSizeColumn(2);
for (int i = 0; i <= 5; i++) {
count++;
posicaoExcel = "longgggggggggggggggggggggggggggg";
row = sheet.createRow(count);
Cell cellSer = row.createCell(0);
// cellSer = row.createCell(0);
String myCellSer = posicaoExcel;
String stringCellValueSer = myCellSer;
cellSer.setCellValue(stringCellValueSer);
cellSer.setCellStyle(style);
// sheet.autoSizeColumn(1);
Cell cellnf = row.createCell(1);
//cellnf = row.createCell(1);
String myCell = posicaoExcel;
String stringCellValue = myCell;
cellnf.setCellValue(stringCellValue);
cellnf.setCellStyle(style);
// sheet.autoSizeColumn(2);
}
//loop para alterar o tamanho da coluna
int t = count;
for (int i = 0; i < t; i++) {
sheet.autoSizeColumn(t);
}
try {
wb.write(fos);
} catch (IOException ex) {
Logger.getLogger(XLSTest.class.getName()).log(Level.SEVERE, null, ex);
}
try {
fos.flush();
} catch (IOException ex) {
Logger.getLogger(XLSTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}