मैं एक ऐसा फ़ंक्शन बना रहा हूं जो एक्सेल फ़ाइल में उपलब्ध डेटा को पढ़ता है।
मैं स्प्रिंग प्रोजेक्ट के साथ अपाचे पीओआई का उपयोग करता हूं।
हालांकि, मुझे मर्ज सेल के साथ पढ़ने में परेशानी हो रही है। चित्र में दिखाए गए अनुसार सामग्री को लाल रंग में हाइलाइट करने के लिए मुझे जो चाहिए वह है।
क्या आप मुझे कोड सुझा सकते हैं?
धन्यवाद !
अद्यतन:
`
public void importClassroomByExcel(MultipartFile file) {
try {
Workbook workbook = new HSSFWorkbook(file.getInputStream());
Iterator<Sheet> sheetIterator = workbook.iterator();
while (sheetIterator.hasNext()) {
Sheet sheet = sheetIterator.next();
String title = sheet.getRow(4).getCell(5).toString();
}
workbook.close();
} catch (Exception e) {
throw new BadRequestAlertException(ErrorConstants.DEFAULT_TYPE, "", e.getMessage(), "filestructerror",
Status.BAD_REQUEST);
}
}
`
1 उत्तर
सुनिश्चित नहीं है कि मैं आपकी आवश्यकता को सही समझता हूं। मुझे लगता है कि आप यही चाहते हैं: आपकी शीट में, डेटा श्रेणी पंक्ति 5 से शुरू होती है और कॉलम ए समूहीकरण निर्धारित करता है। अब जरूरत प्रत्येक समूह के लिए कॉलम F और G के मान प्राप्त करने की है। फिर जरूरत कॉलम एन में प्रत्येक समूह के लिए पहली और कॉलम ओ में प्रत्येक समूह के लिए अंतिम तिथि प्राप्त करने की है।
इसलिए प्रत्येक समूह के लिए पहली पंक्ति और प्रत्येक समूह के लिए अंतिम भरी हुई पंक्ति प्राप्त करने की आवश्यकता है। निम्नलिखित उदाहरण यह दिखाता है। मुख्य रूप से यह एक विधि int getNextFilledRow(Sheet sheet, int columnNum, int startRowNum)
का उपयोग करता है ताकि दी गई पंक्ति संख्या से शुरू होने वाले शीट के दिए गए कॉलम में अगली भरी हुई पंक्ति प्राप्त हो सके।
कोड को केवल आपकी दी गई शीट में लाल चिह्नित सामग्री वाले सेल मिलना चाहिए।
import java.io.FileInputStream;
import org.apache.poi.ss.usermodel.*;
class ReadExcelMergedCells {
static int getNextFilledRow(Sheet sheet, int columnNum, int startRowNum) {
int lastRow = sheet.getLastRowNum();
int nextFilledRow = lastRow;
for (int r = startRowNum; r <= lastRow; r++) {
nextFilledRow = r + 1;
Row row = sheet.getRow(nextFilledRow);
Cell cell = (row != null)?cell = row.getCell(columnNum):null;
if (cell != null && cell.getCellType() != CellType.BLANK) {
break;
}
}
return nextFilledRow;
}
public static void main(String[] args) throws Exception {
Workbook workbook = WorkbookFactory.create(new FileInputStream("./Excel.xlsx"));
Sheet sheet = workbook.getSheetAt(0);
int lastRow = sheet.getLastRowNum();
int startRow = 4;
Row row = null;
Cell cell = null;
int columnNum = -1;
int columNumA = 0; //column A determines the grouping
for (int r = startRow; r <= lastRow; r = getNextFilledRow(sheet, columNumA, r)) {
//r is the first row in this group
System.out.println("Group starting in row " + r);
//get cell in column F of first row in this group
columnNum = 5;
row = sheet.getRow(r);
cell = (row != null)?cell = row.getCell(columnNum):null;
System.out.println(cell);
//get cell in column G of first row in this group
columnNum = 6;
row = sheet.getRow(r);
cell = (row != null)?cell = row.getCell(columnNum):null;
System.out.println(cell);
//get cell in column N of first row in this group
columnNum = 13;
row = sheet.getRow(r);
cell = (row != null)?cell = row.getCell(columnNum):null;
System.out.println(cell);
//get cell in column O of last filled row in this group
columnNum = 14;
int lastRowInGroup = getNextFilledRow(sheet, columNumA, r) -1;
// get last filled cell in this group
for (int rl = lastRowInGroup; rl >= r; rl--) {
row = sheet.getRow(rl);
cell = (row != null)?cell = row.getCell(columnNum):null;
if (cell != null && cell.getCellType() != CellType.BLANK) {
break;
}
}
System.out.println(cell);
System.out.println("Group ending in row " + lastRowInGroup);
}
workbook.close();
}
}
संबंधित सवाल
नए सवाल
java
जावा एक उच्च स्तरीय प्रोग्रामिंग भाषा है। इस टैग का उपयोग तब करें जब आपको भाषा का उपयोग करने या समझने में समस्या हो। इस टैग का उपयोग शायद ही कभी किया जाता है और इसका उपयोग अक्सर [वसंत], [वसंत-बूट], [जकार्ता-ई], [Android], [javafx], [हडूप], [श्रेणी] और [मावेन] के साथ किया जाता है।