0 Replies - 121 Views - Last Post: 04 April 2013 - 01:57 PM Rate Topic: -----

#1 billy.stalnaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-April 13

I have a problem: Java Excel POI api autoSizeColumn(int s) method

Posted 04 April 2013 - 01:57 PM

Ok so I feel like i have already exhausted all my efforts I have posted this code on many different sites but no one has been able to help me. The problem is that after I write to an excel file I want to call the autoSizeColumn() method on the columns (after I have entered all my data!!) I have tried moving the loop that does the auto sizing around but it either sizes to the last cell entered which is usually shorter than the first cell or sizing to the default width.. here is the code.. The .png attached is what happens.
				public void writeFile() {
					Workbook wb = new HSSFWorkbook();
					Sheet s = wb.createSheet();

					try {
						FileOutputStream out = new FileOutputStream(
								this.getSelectedFile());
						// declare a row object reference
						Row r = null;
						// declare a cell object reference
						Cell c = null;
						// in case of plain ascii
						wb.setSheetName(0, "Street Light Report");
						// create a sheet with 30 rows (0-29)
						String[] colName = { "Light Id", "Flagged",
								"Malfunction", "Comments", "Location", "Date" };

						String[][] data = Connections.searchForFlagged();
						for (int i = 0; i < data.length; i++) {
							r = s.createRow(i);
						}
						r.setRowNum(0);
						for (int j = 0; j < 6; j++) {
							c = r.createCell(j);
							switch (j) {
							case 0:
								c.setCellValue(colName[j]);
								break;
							case 1:
								c.setCellValue(colName[j]);
								break;
							case 2:
								c.setCellValue(colName[j]);
								break;
							case 3:
								c.setCellValue(colName[j]);
								break;
							case 4:
								c.setCellValue(colName[j]);
								break;
							case 5:
								c.setCellValue(colName[j]);
								break;
							}
						}
						for (int i = 0; i < data.length; i++) {
							r.setRowNum(i + 1);
							for (int j = 0; j < data[i].length; j++) {
								c = r.createCell(j);
								switch (j) {
								case 0:
									c.setCellType(Cell.CELL_TYPE_NUMERIC);
									c.setCellValue(Integer.parseInt(data[i][j]));
									break;
								case 1:
									if (data[i][j].equals("true"))
										c.setCellValue("Yes");
									else
										c.setCellValue("No");
									break;
								case 2:
									if (data[i][j].equals("I"))
										c.setCellValue("Intermittent");
									else
										c.setCellValue("Not Functional");
									break;
								case 3:
									c.setCellValue(data[i][j]);
									break;
								case 4:
									c.setCellValue(data[i][j]);
									break;
								case 5:
									c.setCellValue(data[i][j]);
									break;
								}
							}
						}
						s.setPrintGridlines(true);
						s.setDisplayGridlines(true);
						// for (int j = 0; j < 5; j++) {
						// s.autoSizeColumn(j);
						// }
						wb.write(out);
						out.close();
					} catch (Exception ex) {
					}
					autoSize(s);
				}

				public void autoSize(Sheet s) {
					for (int j = 0; j < 5; j++) {
						s.autoSizeColumn(j);
					}
				}
			};


Is This A Good Question/Topic? 0
  • +

Page 1 of 1