I thought I had created 2 styling objects.... Below is the full code sample... Could you please let me know, how I should have created the second cellstyle object... Or more to the point, how would I copy the attributes of one object to my new object?
I also I tried this, with no avail.... Using the same cell styling object, but just twiddling an attribute... so that's when I went to the 2 cell styling objects...
Question # 1: Should this work?
if (row == 5) {
cs.setRotation((short) 90);
c.setCellStyle(cs);
}
else {
cs.setRotation((short) 0);
c.setCellStyle(cs);
}
Question #2: Any thoughts on what I should change to make the following code work for conditional cell stylings?
Thanks, tim
package net.sourceforge.poi.hssf.usermodel;
import java.io.*;
import java.util.*;
import java.sql.*;
import java.math.BigDecimal;
import java.math.*;
import junit.framework.*;
import net.sourceforge.poi.poifs.filesystem.Filesystem;
import junit.framework.TestCase;
public class TestCellStyle
extends TestCase {
public TestCellStyle(String name) {
super(name);
}
public void testWriteSheetStyle()
throws IOException {
File file = new File("jdbcexceltest.xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
HSSFRow r = null;
HSSFCell c = null;
HSSFFont fnt = wb.createFont();
HSSFCellStyle cs = wb.createCellStyle();
HSSFCellStyle cs2 = wb.createCellStyle();
s.setColumnWidth((short) 1, (short) (256*30));
cs.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cs.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cs.setBorderRight(HSSFCellStyle.BORDER_THIN);
cs.setBorderTop(HSSFCellStyle.BORDER_THIN);
cs.setFillForegroundColor(HSSFCellStyle.YELLOW);
cs.setFillPattern(HSSFCellStyle.AQUA);
fnt.setColor(HSSFFont.RED);
fnt.setItalic(true);
cs.setFont(fnt);
cs2 = cs;
cs2.setRotation((short) 90);
cs2.setFillForegroundColor(HSSFCellStyle.GREEN);
fnt.setColor(HSSFFont.GREEN);
fnt.setItalic(true);
cs2.setFont(fnt);
short row =0;
short cell =0;
Connection con = null;
ResultSetMetaData rsmd = null;
try {
DriverManager.registerDriver(new com.ibm.as400.access.AS400JDBCDriver());
con = DriverManager.getConnection("jdbc:as400://xxx.xxx.x.xx", "", "");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM QIWS.QCUSTCDT ORDER BY LSTNAM");
rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();
int rowCount = 0;
for (int i = 1; i <= colCount; i++) {
System.out.println("Information about column " + i);
System.out.println(" Name..........: " + rsmd.getColumnName(i));
System.out.println(" Data Type.....: " + rsmd.getColumnType(i) + " ( " + rsmd.getColumnTypeName(i) + " )");
System.out.println(" Precision.....: " + rsmd.getPrecision(i));
System.out.println(" Scale.........: " + rsmd.getScale(i));
System.out.print (" Allows Nulls..: ");
if (rsmd.isNullable(i)==0)
System.out.println("false");
else
System.out.println("true");
}
while (rs.next()) {
r = s.createRow(row);
if (row == 2) {
r.setHeight((short) 1500);
}
// Cell 1
cell = 0;
c = r.createCell(cell, HSSFCell.CELL_TYPE_STRING);
c.setCellValue(rs.getString("CUSNUM"));
// Cell 2
cell++;
c = r.createCell(cell, HSSFCell.CELL_TYPE_STRING);
c.setCellValue(rs.getString("LSTNAM"));
if (row == 5) {
c.setCellStyle(cs);
}
else {
c.setCellStyle(cs2);
}
// Cell 3
cell++;
c = r.createCell(cell, HSSFCell.CELL_TYPE_STRING);
c.setCellValue(rs.getString("STATE"));
// Cell 4
cell++;
c = r.createCell(cell, HSSFCell.CELL_TYPE_STRING);
c.setCellValue(rs.getString("BALDUE"));
row++;
}
rs.close();
stmt.close();
} catch (Exception e) {
System.out.println("\nERROR: " + e.getMessage());
} finally {
try {
wb.write(out);
out.close();
con.close();
} catch (SQLException e) {
}
}
System.exit(0);
}
public static void main(String[] ignored_args) {
System.out.println(
"Testing net.sourceforge.poi.hssf.usermodel.HSSFCellStyle");
junit.textui.TestRunner.run(TestCellStyle.class);
}
}
-----Original Message-----
From: Laubach, Shawn - TAFB/LAB NCC (SAIC) [SMTP:[EMAIL PROTECTED]]
Sent: Monday, March 25, 2002 10:35 AM
To: '[EMAIL PROTECTED]'
Cc: '[EMAIL PROTECTED]'
Subject: RE: Conditional Cell Styling
You need to create a completely new cell style option for cs2.� You are just copying the pointer in the statement cs2 = cs so that cs and cs2 points to the same object.
�
Shawn Laubach
SAIC - Web Developer
-----Original Message-----
From: Hatzenbeler, Tim [mailto:[EMAIL PROTECTED]]
Sent: Monday, March 25, 2002 12:15 PM
To: '[EMAIL PROTECTED]'
Subject: FW: Conditional Cell Styling
I was trying to test the rotation feature, and I can't seem to make this work using this setup.
I was using (2) different cellstyling object, and then on a praticular row, set styling to cs, and on the other rows set it to cs2, but it seems that the cs2 object overwrites the cs object when it write the spreadsheet.�
Thanks, tim
������� s.setColumnWidth((short) 1, (short) (256*30));
������� cs.setBorderBottom(HSSFCellStyle.BORDER_THIN);
������� cs.setBorderLeft(HSSFCellStyle.BORDER_THIN);
������� cs.setBorderRight(HSSFCellStyle.BORDER_THIN);
������� cs.setBorderTop(HSSFCellStyle.BORDER_THIN);
������� cs.setFillForegroundColor(HSSFCellStyle.YELLOW);
������� cs.setFillPattern(HSSFCellStyle.AQUA);
�������
������� fnt.setColor(HSSFFont.RED);
������� fnt.setItalic(true);
������� cs.setFont(fnt);
�������
������� cs2 = cs;
������� cs2.setRotation((short) 90);
������� cs2.setFillForegroundColor(HSSFCellStyle.GREEN);
������� fnt.setColor(HSSFFont.GREEN);
������� fnt.setItalic(true);
������� cs2.setFont(fnt);
.....
����������� while (rs.next()) {
��������������� r = s.createRow(row);
���������������
��������������� if (row == 2) {��������������������������������� /**** this worked ****/
��������������������� r.setHeight((short) 1500);
������������������� }
������������������������������ cell++;
������������������������������ c = r.createCell(cell, HSSFCell.CELL_TYPE_STRING);
������������������������������ c.setCellValue(rs.getString("LSTNAM"));
��������
/***** Problem ***/��������������� if (row == 5) {���������������������������������
������������������������������������������������������������ c.setCellStyle(cs);
��������������������������� }
�������������������� else {
������������������������������������������������������������ c.setCellStyle(cs2);
����������������� }
