5d721750ca42f9f10cde5102f0a0dd2c298452f6.svn-base 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package xin.glue.ui.common.blob;
  2. import java.io.InputStream;
  3. import java.io.OutputStream;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import com.posdata.glue.dao.vo.PosRowSet;
  7. import jxl.Cell;
  8. import jxl.Sheet;
  9. import jxl.Workbook;
  10. import jxl.write.Label;
  11. import jxl.write.WritableSheet;
  12. import jxl.write.WritableWorkbook;
  13. import jxl.write.WriteException;
  14. import jxl.write.biff.RowsExceededException;
  15. public abstract class PosExcelEngine implements PosExcelInterface {
  16. protected List dataList = new ArrayList();
  17. protected int dataRow = 0;
  18. protected int addRows = 0;
  19. protected int pageSize = 25;
  20. protected int pageCount = 0;
  21. protected boolean autoBlank = true;
  22. public void setData(List dataList, InputStream is, OutputStream os, int dataRow, int pageSize) {
  23. for (int i = 0; i < dataList.size(); i++) {
  24. ArrayList list = new ArrayList();
  25. PosRowSet rowSet = (PosRowSet)dataList.get(i);
  26. while (rowSet.hasNext())
  27. list.add(rowSet.next().getAttributes());
  28. this.dataList.add(list);
  29. }
  30. this.dataRow = dataRow;
  31. this.pageSize = pageSize;
  32. try {
  33. Workbook rwb = Workbook.getWorkbook(is);
  34. WritableWorkbook wb = Workbook.createWorkbook(os, rwb);
  35. setData(rwb, wb);
  36. wb.write();
  37. wb.close();
  38. rwb.close();
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. protected abstract void setData(Workbook rwb, WritableWorkbook wb) throws RowsExceededException, WriteException;
  44. protected int insertRow(WritableSheet sheet, int curRow, Sheet rs, int refRow) throws WriteException {
  45. sheet.insertRow(++curRow); // Ìí¼ÓÐÐ
  46. sheet.setRowView(curRow, rs.getRowView(refRow).getSize());
  47. Cell cell;
  48. Label label;
  49. Cell[] cells = rs.getRow(refRow);
  50. for (int curCol = 0, count = cells.length; curCol < count; curCol++) {
  51. cell = cells[curCol];
  52. label = new Label(curCol, curRow, "", cell.getCellFormat());
  53. sheet.addCell(label);
  54. }
  55. this.addRows++;
  56. return curRow;
  57. }
  58. protected void setValue(WritableSheet sheet, int curRow, int curCol, Object value) {
  59. if (value != null && !value.equals("")) {
  60. Label label;
  61. label = (Label)sheet.getWritableCell(curCol, curRow);
  62. String val = label.getContents();
  63. if (val.equals("")) val = value.toString();
  64. else val += '\n' + value.toString(); // '\012' Ç¿ÖÆ»»ÐÐ
  65. label.setString(val);
  66. }
  67. }
  68. protected void addBlankRow(WritableSheet sheet, int curRow, Sheet rs, int refRow, int rows) throws WriteException {
  69. if (autoBlank) {
  70. while (rows < pageSize) {
  71. curRow = insertRow(sheet, curRow, rs, refRow);
  72. }
  73. }
  74. }
  75. }