new faq on cell styles

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352966 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Avik Sengupta 2002-12-27 05:44:40 +00:00
parent 04e78aae67
commit 3b8f9aff10

View File

@ -66,7 +66,6 @@
Thanks to Jason Hoffman for providing the solution. Thanks to Jason Hoffman for providing the solution.
<source> <source>
case HSSFCell.CELL_TYPE_NUMERIC: case HSSFCell.CELL_TYPE_NUMERIC:
double d = cell.getNumericCellValue(); double d = cell.getNumericCellValue();
// test if a date! // test if a date!
@ -78,9 +77,7 @@
cellText = cal.get(Calendar.MONTH)+1 + "/" + cellText = cal.get(Calendar.MONTH)+1 + "/" +
cal.get(Calendar.DAY_OF_MONTH) + "/" + cal.get(Calendar.DAY_OF_MONTH) + "/" +
cellText; cellText;
} } </source>
</source>
</answer> </answer>
</faq> </faq>
<faq> <faq>
@ -93,7 +90,7 @@
screen. The problem persists even though you have set the correct mime type. screen. The problem persists even though you have set the correct mime type.
</p> </p>
<p> <p>
The short answer is, dont depend on IE to display a binary file type you an attachment properly if you stream it via a The short answer is, dont depend on IE to display a binary file type properly if you stream it via a
servlet. Every minor version of IE has different bugs on this issue. servlet. Every minor version of IE has different bugs on this issue.
</p> </p>
<p> <p>
@ -112,9 +109,8 @@
<p> <p>
To guarantee opening the file properly in Excel from IE, write out your file to a To guarantee opening the file properly in Excel from IE, write out your file to a
temporary file under your web root from your servelet. Then send an http response temporary file under your web root from your servelet. Then send an http response
to the browser to do a client side redirection to your temp file. (If you do a to the browser to do a client side redirection to your temp file. (Note that using a
server side redirect using RequestDispatcher, you will have to add .xls to the server side redirect using RequestDispatcher will not be effective in this case)
request as mentioned above.)
</p> </p>
<p> <p>
Note also that when you request a document that is opened with an Note also that when you request a document that is opened with an
@ -171,25 +167,17 @@
so to support localization you should use Unicode. so to support localization you should use Unicode.
To do it you should set it manually: To do it you should set it manually:
<source> <source>
//
// for sheet name // for sheet name
//
HSSFWorkbook wb = new HSSFWorkbook(); HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet(); HSSFSheet s = wb.createSheet();
wb.setSheetName( 0, "SomeUnicodeName", HSSFWorkbook.ENCODING_UTF_16 ); wb.setSheetName( 0, "SomeUnicodeName", HSSFWorkbook.ENCODING_UTF_16 );
//
// for cell value // for cell value
//
HSSFRow r = s.createRow( 0 ); HSSFRow r = s.createRow( 0 );
HSSFCell c = r.createCell( (short)0 ); HSSFCell c = r.createCell( (short)0 );
c.setCellType( HSSFCell.CELL_TYPE_STRING ); c.setCellType( HSSFCell.CELL_TYPE_STRING );
c.setEncoding( HSSFCell.ENCODING_UTF_16 ); c.setEncoding( HSSFCell.ENCODING_UTF_16 );
c.setCellValue( "\u0422\u0435\u0441\u0442\u043E\u0432\u0430\u044F" ); c.setCellValue( "\u0422\u0435\u0441\u0442\u043E\u0432\u0430\u044F" ); </source>
</source>
Make sure you make the call to setEncoding() before calling setCellValue(), otherwise what you pass in won't be interpreted properly. Make sure you make the call to setEncoding() before calling setCellValue(), otherwise what you pass in won't be interpreted properly.
</answer> </answer>
</faq> </faq>
@ -202,4 +190,82 @@
Make sure you have fix pack 4 installed. Make sure you have fix pack 4 installed.
</answer> </answer>
</faq> </faq>
<faq>
<question> I am using styles when creating a workbook in POI, but Excel refuses to open the file, complaining about "Too Many Styles".
</question>
<answer>
<p>You just create the styles OUTSIDE of the loop in which you create cells.</p>
<p>GOOD:</p>
<source>
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = null;
// Aqua background
HSSFCellStyle style = wb.createCellStyle();
style.setFillBackgroundColor(HSSFColor.AQUA.index);
style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
HSSFCell cell = row.createCell((short) 1);
cell.setCellValue("X");
cell.setCellStyle(style);
// Orange "foreground", foreground being the fill foreground not the font color.
style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.ORANGE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
for (int x = 0; x &lt; 1000; x++) {
// Create a row and put some cells in it. Rows are 0 based.
row = sheet.createRow((short) k);
for (int y = 0; y &lt; 100; y++) {
cell = row.createCell((short) k);
cell.setCellValue("X");
cell.setCellStyle(style);
}
}
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close(); </source>
<p>BAD:</p>
<source>
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = null;
for (int x = 0; x &lt; 1000; x++) {
// Aqua background
HSSFCellStyle style = wb.createCellStyle();
style.setFillBackgroundColor(HSSFColor.AQUA.index);
style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
HSSFCell cell = row.createCell((short) 1);
cell.setCellValue("X");
cell.setCellStyle(style);
// Orange "foreground", foreground being the fill foreground not the font color.
style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.ORANGE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
// Create a row and put some cells in it. Rows are 0 based.
row = sheet.createRow((short) k);
for (int y = 0; y &lt; 100; y++) {
cell = row.createCell((short) k);
cell.setCellValue("X");
cell.setCellStyle(style);
}
}
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close(); </source>
</answer>
</faq>
</faqs> </faqs>