2012-05-27 10 views
10

मैं एक सेल शैली को डिफेंडर woekbooks पर लागू करने की कोशिश कर रहा हूं। यह अच्छी तरह से काम करता है, जब मैं इसे पहली कार्यपुस्तिका पर लागू करता हूं, लेकिन जब मैं इसे दूसरी और अगली कार्यपुस्तिकाओं के साथ करने की कोशिश कर रहा हूं - कोई शैली लागू नहीं होती है और निम्नलिखित अपवाद फेंक दिया जाता है।अपाचे पोई विभिन्न कार्यपुस्तिकाओं में एक शैली लागू करें

Exception in thread "Thread-3" java.lang.IllegalArgumentException: This Style does not belong to the supplied Workbook Stlyes Source. Are you trying to assign a style from one workbook to the cell of a differnt workbook? 
    at org.apache.poi.xssf.usermodel.XSSFCellStyle.verifyBelongsToStylesSource(XSSFCellStyle.java:118) 
    at org.apache.poi.xssf.usermodel.XSSFCell.setCellStyle(XSSFCell.java:500) 
    at CoreLayer.ExportManager.ExcelExproter.applyStyle(ExcelExproter.java:224) 
    at CoreLayer.ExportManager.ExcelExproter.groupSchedule(ExcelExproter.java:47) 
    at UILayer.ExportDialog$ExportWorker.run(ExportDialog.java:111) 
    at java.lang.Thread.run(Thread.java:722) 

निम्नलिखित कोड प्रयोग किया जाता है:

public void professorSchedule(Professor professor) { 
     Workbook wb = new XSSFWorkbook(); 
     Sheet sheet = wb.createSheet(TextConstants.SCHEDULE); 
     String safeName = WorkbookUtil.createSafeSheetName(professor.toString() + ".xlsx"); 

     LinkedHashMap<ScheduleSlot, Lesson> professorSchedule = data.getSchedule().getProfessorSchedule(professor); 
     fillProfessorSchedule(sheet, professorSchedule); 

     applyStyle(wb, sheet); 
     try { 
      FileOutputStream fileOutputStream = new FileOutputStream(settings.getSchedulesPath() + safeName); 
      wb.write(fileOutputStream); 
      fileOutputStream.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    private void applyStyle(Workbook wb, Sheet sheet) { 
     CellStyle style = wb.createCellStyle(); 
     style.setWrapText(true); 

     int columnNumber = 0; 
     sheet.autoSizeColumn(columnNumber); 
     for (Row row : rows) { 
      for (Cell cell : row) { 
       cell.setCellStyle(style); 
       sheet.setColumnWidth(columnNumber++, 5000); 
      } 
     } 
    } 

अग्रिम धन्यवाद हर कोई!

उत्तर

16

आप ऐसा नहीं कर सकते हैं, सेल स्टाइल ऑब्जेक्ट्स एक कार्यपुस्तिका के लिए विशिष्ट हैं। वे काफी गहरी वस्तुएं हैं, और अधिकांश शैली कार्यपुस्तिका में आयोजित की जाती है, इसलिए आप फिर से उपयोग नहीं कर सकते हैं। आपको एक उपयोगी अपवाद भी मिलता है जो आपको यह बताता है!

शैली के विवरण की प्रतिलिपि बनाने के लिए आपको cloneStyleFrom(CellStyle) विधि का उपयोग करने की आवश्यकता है। कुछ ऐसा:

Workbook wb = WorkbookFactory.create(new File("existing.xls")); 
CellStyle origStyle = wb.getCellStyleAt(1); // Or from a cell 

Workbook newWB = new XSSFWorkbook(); 
Sheet sheet = newWB.createSheet(); 
Row r1 = sheet.createRow(0); 
Cell c1 = r1.createCell(0); 

CellStyle newStyle = newWB.createCellStyle(); 
newStyle.cloneStyleFrom(origStyle); 
c1.setCellStyle(newStyle); 

newWB.write(new FileOutpuStream("new.xlsx")); 
+0

लेकिन जैसा कि आप देख सकते हैं कि मैं प्रत्येक पुस्तक के लिए एक नई शैली तैयार करता हूं। असल में मुझे नहीं पता कि क्या कोई शैलियों का निर्माण होता है और मुझे नहीं लगता कि आपके द्वारा प्रदान किया गया qpproach मेरे मामले के लिए उपयुक्त है। क्या यह सिर्फ प्रत्येक पुस्तक के लिए एक नई शैली बनाने के लिए संभव है? और मेरे कोड में क्या गलती है? आपकी मदद के लिए बहुत बहुत शुक्रिया। –

+3

आपको यह सुनिश्चित करने की ज़रूरत है कि आप केवल उस कार्यपुस्तिका के साथ शैली का उपयोग करें जिसे आपने बनाया है। आप या तो कार्यपुस्तिका (और ट्रैक!) प्रति शैलियों को बना सकते हैं, या इसे एक कार्यपुस्तिका के लिए बना/लोड कर सकते हैं और इसे बाद में क्लोन कर सकते हैं – Gagravarr

संबंधित मुद्दे