How can I crawl a website with multiple pages using Java? - java

Following code works fine but it only fetch data from one page. How can I handle pagination so that the code can fetch data from the first page then second and continue until the last page number?
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import com.webscrap4j.WebScrap;
import com.webscrap4j.WebScrapException;
public class okayapower_battery {
#SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
ArrayList<String> al = new ArrayList<String>();
ArrayList<String> bl = new ArrayList<String>();
ArrayList<String> cl = new ArrayList<String>();
WebScrap ws = new WebScrap();
ws.setUrl("https://www.okayapower.com/product-category/inverter/");
try
{
ws.startWebScrap();
//al = ws.getImageTagData("img", "title");
al = ws.getSingleHTMLScriptData("<h3>", "</h3>");
bl = ws.getSingleHTMLScriptData("<del>", "</del>");
cl = ws.getSingleHTMLScriptData("<ins>", "</ins>");
HSSFWorkbook workBook = new HSSFWorkbook();
FileOutputStream fos = new FileOutputStream("/Users/parthpatil/Documents/Abm Technologies/Crawl/okaya_battery.xls"); {
// Create the Sheet
HSSFSheet Sheet = workBook.createSheet("products");
// Create the first row corresponding to the header
Row header = Sheet.createRow(0);
header.createCell(0).setCellValue("Product Name");
header.createCell(1).setCellValue("Product Price");
header.createCell(2).setCellValue("Product MRP");
// Ensure that all the List have the same size otherwise throw an exception
if (al.size() != bl.size() || al.size() != cl.size())
throw new IllegalStateException("Some data is missing");
// Iterate over all the list an create the rows of data
for(int i = 0; i < al.size(); i++){
// Create the current starting from 1 to al.size()
HSSFRow row = Sheet.createRow((short) i + 1);
// Cell of the Product Name
row.createCell(0).setCellValue(al.get(i));
// Cell of the Product Price
row.createCell(1).setCellValue(cl.get(i));
// Cell of the Product MRP
row.createCell(2).setCellValue(bl.get(i));
}
// Write the result into the file
workBook.write(fos);
for (String adata : al)
{
System.out.println("the product are:- " + adata);
}
for (String bdata : bl)
{
System.out.println("the MRp are:- " + bdata);
}
for (String cdata : cl)
{
System.out.println("the selling price is:- " + cdata);
}
}
}catch (WebScrapException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Just add simple for loop like for(int page=1; page<=10; page++) { (where 10 is the last page you want to parse) just before ws.setUrl(... which should be as well changed to ws.setUrl("https://www.okayapower.com/product-category/inver‌​ter/page/" + String.valueOf(page) + "/");
So the resultant code should look like:
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import com.webscrap4j.WebScrap;
import com.webscrap4j.WebScrapException;
public class okayapower_battery {
#SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
ArrayList<String> al = new ArrayList<String>();
ArrayList<String> bl = new ArrayList<String>();
ArrayList<String> cl = new ArrayList<String>();
WebScrap ws = new WebScrap();
for(int page=1; page<=10; page++) {
ws.setUrl("https://www.okayapower.com/product-category/inverter/page/" + String.valueOf(page) + "/");
try {
ws.startWebScrap();
//al = ws.getImageTagData("img", "title");
al = ws.getSingleHTMLScriptData("<h3>", "</h3>");
bl = ws.getSingleHTMLScriptData("<del>", "</del>");
cl = ws.getSingleHTMLScriptData("<ins>", "</ins>");
HSSFWorkbook workBook = new HSSFWorkbook();
FileOutputStream fos = new FileOutputStream("/Users/parthpatil/Documents/Abm Technologies/Crawl/okaya_battery.xls"); {
// Create the Sheet
HSSFSheet Sheet = workBook.createSheet("products");
// Create the first row corresponding to the header
Row header = Sheet.createRow(0);
header.createCell(0).setCellValue("Product Name");
header.createCell(1).setCellValue("Product Price");
header.createCell(2).setCellValue("Product MRP");
// Ensure that all the List have the same size otherwise throw an exception
if (al.size() != bl.size() || al.size() != cl.size())
throw new IllegalStateException("Some data is missing");
// Iterate over all the list an create the rows of data
for(int i = 0; i < al.size(); i++){
// Create the current starting from 1 to al.size()
HSSFRow row = Sheet.createRow((short) i + 1);
// Cell of the Product Name
row.createCell(0).setCellValue(al.get(i));
// Cell of the Product Price
row.createCell(1).setCellValue(cl.get(i));
// Cell of the Product MRP
row.createCell(2).setCellValue(bl.get(i));
}
// Write the result into the file
workBook.write(fos);
for (String adata : al)
{
System.out.println("the product are:- " + adata);
}
for (String bdata : bl)
{
System.out.println("the MRp are:- " + bdata);
}
for (String cdata : cl)
{
System.out.println("the selling price is:- " + cdata);
}
}
} catch (WebScrapException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

Related

Set number format on BarChart Label in word document using Apache POI

I am trying to update already existing Bar Chart in word document using Apache poi.
Before Updating chart, series already has some values in percentage but after update, not able to see percentage value, After I click on Edit Excel percentage value gets reflected.
For example - I want to Update 80% and 20% so for this I am updating 0.8 and 0.2 in chart's excel source data but in the chart I see 0.8 and 0.2.
What I want ? - After updating the Bar chart it should show labels in percentage without the need of refresh or Edit Excel Data.
After Update (after running the code)
After clicking on Edit Excel Data
Excel Data is as below
Code
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Iterator;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xddf.usermodel.chart.XDDFCategoryDataSource;
import org.apache.poi.xddf.usermodel.chart.XDDFChartData;
import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory;
import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFChart;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
public class ReadWord {
public static void main(String[] args) {
String fileName = "D:\\Test - Copy.docx";
try (XWPFDocument doc = new XWPFDocument(Files.newInputStream(Paths.get(fileName)))) {
XWPFChart chart = null;
for (POIXMLDocumentPart part : doc.getRelations()) {
if (part instanceof XWPFChart) {
chart = (XWPFChart) part;
break;
}
}
XSSFWorkbook chartDataWorkbook = chart.getWorkbook();
String sheetName = chartDataWorkbook.getSheetName(0);
XSSFSheet chartDataSheet = chartDataWorkbook.getSheet(sheetName);
Iterator<Row> itr = chartDataSheet.iterator();
while (itr.hasNext()) {
Row row = itr.next();
Iterator<Cell> cellIterator = row.cellIterator(); // iterating over each column
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case STRING: // field that represents string cell type
System.out.print(cell.getStringCellValue() + "\t\t\t");
break;
case NUMERIC: // field that represents number cell type
System.out.print(cell.getNumericCellValue() + "\t\t\t");
break;
default:
}
}
System.out.println("");
}
Object[][] data = new Object[][] { // 1 series, 1 categories
{ "", "RECYCLED", "REFURISHED" }, // series titles
{ "DESKTOP", 0.20d, 0.80d }, // category 1
};
//System.out.println("Data -> "+chart.getCTChart().getPlotArea().getBarChartArray(0).getSerArray(0).getDLbls().getNumFmt());
int rMin = 1;
int rMax = 1;
int c = 0;
XDDFCategoryDataSource category = null;
XDDFChartData chartData = chart.getChartSeries().get(0);
for (int r = rMin; r < rMax + 1; r++) {
chartDataSheet.getRow(r).getCell(c).setCellValue((String) data[r][c]); // in sheet
}
category = XDDFDataSourcesFactory.fromStringCellRange(chartDataSheet, new CellRangeAddress(1, 1, c, c)); // in
// chart
// Series 1
XDDFChartData.Series series1 = chartData.getSeries().get(0);
c = 1;
String series1Title = (String) data[0][c];
chartDataSheet.getRow(0).getCell(c).setCellValue(series1Title); // in sheet
if (chartDataSheet.getTables().get(0).getCTTable().getTableColumns().getTableColumnList().size() > c) {
chartDataSheet.getTables().get(0).getCTTable().getTableColumns().getTableColumnList().get(c)
.setName(series1Title);
}
series1.setTitle(series1Title, new CellReference(sheetName, 0, c, true, true)); // in chart
/*CellStyle style = chartDataWorkbook.createCellStyle();
style.setDataFormat(chartDataWorkbook.createDataFormat().getFormat("0.0%"));*/
// set new values
XDDFNumericalDataSource<Double> values = null;
for (int r = rMin; r < rMax+1; r++) {
System.out.println("r->" + r + ", c->" + c);
System.out.println("Data -> " + (Double) data[c][r]);
chartDataSheet.getRow(r).getCell(c).setCellValue((Double) data[r][c]); // in sheet
//chartDataSheet.getRow(r).getCell(c).setCellStyle(style);
}
System.out.println("rMin->" + rMin + ",rMax->" + rMax + ",c->" + c + ",c->" + c);
values = XDDFDataSourcesFactory.fromNumericCellRange(chartDataSheet,
new CellRangeAddress(rMin, rMax, c, c));
// values = XDDFDataSourcesFactory.fromNumericCellRange(chartDataSheet, new
// CellRangeAddress(1,1,1,2));
series1.replaceData(category, values);
series1.plot(); // in chart
// series 2
// series 2
XDDFChartData.Series series2 = chartData.getSeries().get(1);
c = 2;
// set new title
String series2Title = (String) data[0][c];
chartDataSheet.getRow(0).getCell(c).setCellValue(series2Title); // in sheet
if (chartDataSheet.getTables().size() > 0) {
if (chartDataSheet.getTables().get(0).getCTTable().getTableColumns().getTableColumnList().size() > c)
chartDataSheet.getTables().get(0).getCTTable().getTableColumns().getTableColumnList().get(c)
.setName(series2Title);
}
series2.setTitle(series2Title, new CellReference(sheetName, 0, c, true, true)); // in chart
// set new values
for (int r = rMin; r < rMax + 1; r++) {
chartDataSheet.getRow(r).getCell(c).setCellValue((Double) data[r][c]); // in sheet
//chartDataSheet.getRow(r).getCell(c).setCellStyle(style);
}
values = XDDFDataSourcesFactory.fromNumericCellRange(chartDataSheet,
new CellRangeAddress(rMin, rMax, c, c));
series2.replaceData(category, values);
series2.plot(); // in chart
chart.getCTChart().getPlotArea().getBarChartArray(0).getSerArray(0).getDLbls().addNewNumFmt();
chart.getCTChart().getPlotArea().getBarChartArray(0).getSerArray(0).getDLbls().getNumFmt().setSourceLinked(false);
chart.getCTChart().getPlotArea().getBarChartArray(0).getSerArray(0).getDLbls().getNumFmt().setFormatCode("0%");
FileOutputStream out = new FileOutputStream(fileName);
doc.write(out);
out.close();
doc.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Printing out results in a map loading contents from a xlsx file using POI

so I need to print out the product id and image url from a excel file as I need to modify another file with the contents I retrieve, however for some reason the mappings is empty, but if I print the values out directly it shows them correctly.
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
public class Main {
static final Map<String, String> PRODUCT_CODES_BY_IMAGES = new HashMap<>();
public static void main(String[] args) throws IOException, InvalidFormatException {
File file = new File("./data/stock_met_imageurls.xlsx");
FileInputStream fis = new FileInputStream(file);
Workbook workbook = new XSSFWorkbook(fis);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
while (iterator.hasNext()) {
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if(cell != null && cell.getCellTypeEnum().equals(CellType.STRING)) {
String image_url = null;
String product_id = null;
switch(cell.getColumnIndex()) {
case 0:
image_url = cell.getStringCellValue();
break;
case 2:
product_id = cell.getStringCellValue();
break;
}
if(image_url != null && product_id != null) {
PRODUCT_CODES_BY_IMAGES.put(product_id, image_url);
}
}
}
}
PRODUCT_CODES_BY_IMAGES.forEach((k, v) -> {
System.out.println("key = " + k);
System.out.println("val = " + v);
});
System.out.println("Size = " + PRODUCT_CODES_BY_IMAGES.size());
}
}
if I did under the cases
System.out.println("val = " + cell.getStringCellValue());
it prints it out correctly but for some reason the mappings is empty?
Any help would be greatly appreciated.
Because your map is declared as static final. Check out https://www.baeldung.com/java-final
Fixed, I added the elements in the mappings above the wrong bracket.

For-loop prints only the last value of Array through whole Excel file

I am struggling through this IBAN Checker JAVA project where I am supposed to take IBANs from an excel sheet -> validate them -> print valid/invalid results back into Excel. I have it almost all set up but now I got stuck on looping function that should put already validated IBANS back to the sheet. Here I am getting only last IBAN number of my IBAN Array which gets printed into all the rows, other IBANs are not showing.
However, when I use "System.out.printf("%s is %s.%n", iban, validateIBAN(iban) ? "valid" : "not valid");" function all the ibans are validated correctly and printed into console one by one.
Is there please some way to get the results of above "System.out.printf" and iterate them through the cells in the Excel sheet? Or would you have some suggestion for modifications of the for-loop please? I think something in the loop is causing the issue because when I put "System.out.prinf" function inside of the loop, it starts validating only the last IBAN number which means something with the loop is not right.
Thank you very much for any help you can give!
IBANChecker03.java
package ibanchecker03;
import java.math.BigInteger;
import java.util.*;
//EXCEL
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
//EXCEL
public class IBANChecker03 {
private static final String DEFSTRS = ""
+ "AL28 AD24 AT20 AZ28 BE16 BH22 BA20 BR29 BG22 "
+ "HR21 CY28 CZ24 DK18 DO28 EE20 FO18 FI18 FR27 GE22 DE22 GI23 "
+ "GL18 GT28 HU28 IS26 IE22 IL23 IT27 KZ20 KW30 LV21 LB28 LI21 "
+ "LT20 LU20 MK19 MT31 MR27 MU30 MC27 MD24 ME22 NL18 NO15 PK24 "
+ "PS29 PL28 PT25 RO24 SM27 SA24 RS22 SK24 SI19 ES24 SE24 CH21 "
+ "TN24 TR26 AE23 GB22 VG24 GR27 CR21";
private static final Map<String, Integer> DEFINITIONS = new HashMap<>();
static {
for (String definition : DEFSTRS.split(" "))
DEFINITIONS.put(definition.substring(0, 2), Integer.parseInt(definition.substring(2)));
}
public static void printValid(String iban) throws FileNotFoundException, IOException, InvalidFormatException {
File file = new File("G:\\AR\\INVprint (GD Thomas)\\EKG.xls");
FileInputStream ExcelFile = new FileInputStream(file);
HSSFWorkbook wb = new HSSFWorkbook(ExcelFile);
HSSFSheet sheet = wb.getSheet("SheetF");
System.out.printf("%s is %s.%n", iban, validateIBAN(iban) ? "valid" : "not valid");
try {
for (int rowNumber = 0; rowNumber < sheet.getLastRowNum(); rowNumber++) {
HSSFRow row1 = sheet.getRow(rowNumber);
HSSFCell cell = row1.createCell(1);
cell.setCellValue(iban + " is " + validateIBAN(iban));
//System.out.println(cell);
//for(int columnNumber = 1; columnNumber < row1.getLastCellNum();) {
// HSSFCell cell = row1.createCell(columnNumber);
// if(cell != null) {
//cell.setCellValue("Darkness");
//System.out.println(cell);
}
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("G:\\AR\\INVprint (GD Thomas)\\EKG.xls");
wb.write(fileOut);
wb.close();
} catch (FileNotFoundException e) {
throw new FileNotFoundException("File not faaund");
}
}
public static void main(String[] args) throws IOException, FileNotFoundException {
String[] ibans = {"GB33BUKB20201555555555"};
for (String iban : ibans) {
try {
printValid(iban);
} catch (InvalidFormatException ex) {
Logger.getLogger(IBANChecker03.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
static boolean validateIBAN(String iban) {
iban = iban.replaceAll("\\s", "").toUpperCase(Locale.ROOT);
int len = iban.length();
if (len < 4 || !iban.matches("[0-9A-Z]+") || DEFINITIONS.getOrDefault(iban.substring(0, 2), 0) != len)
return false;
iban = iban.substring(4) + iban.substring(0, 4);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i++)
sb.append(Character.digit(iban.charAt(i), 36));
BigInteger bigInt = new BigInteger(sb.toString());
return bigInt.mod(BigInteger.valueOf(97)).intValue() == 1;
}
}
HomeOffice.java
package ibanchecker03;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class HomeOffice {
public static void main(String[] args) throws Exception {
File excelFile = new File("G:\\AR\\INVprint (GD Thomas)\\TEST2 IBAN.xlsx");
FileInputStream fis = new FileInputStream(excelFile);
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(0);
//Here we start iterating through raws and cells
Iterator<Row> rowIt = sheet.iterator();
while (rowIt.hasNext()) {
Row row = rowIt.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if (cell.getColumnIndex() == 7) { // Choose number of column
//System.out.println(cell.toString() + ","); // Print cells
String cellvalue = cell.toString();
IBANChecker03.printValid(cellvalue);
}
}
workbook.close();
fis.close();
}
}
}

how to create dynamic rows of records for creating xls file using list

Here after fetching records from database I have added data in some list and I have set some session variables for them so that I can access in another method by using get(key) method of session which I am successful to do so .Now what I want is I want to create dynamic records by setting this list value in row but I am unable to do so.It creates file but there is no record displayed .Below is my code:
package com.ca.actions;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;
import com.ca.database.Database;
import com.ca.pojo.Event;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
public class DataForGeneralReportsAction extends ActionSupport implements
Preparable, SessionAware {
private List<String> eventsGeneral = new ArrayList<String>();
private List<String> companiesGeneral = new ArrayList<String>();
private SessionMap<String, Object> sessionMapGeneral;
List<String> eventIdList = new ArrayList<String>();
List<String> eventNameList = new ArrayList<String>();
List<String> eventVenueList = new ArrayList<String>();
List<String> eventTimeList = new ArrayList<String>();
List<String> companyNameList = new ArrayList<String>();
List<String> totalAmountList = new ArrayList<String>();
List<String> receivedAmountList = new ArrayList<String>();
List<String> balanceAmountList = new ArrayList<String>();
List<String> eventTdsList = new ArrayList<String>();
List<String> paymentDateList = new ArrayList<String>();
List<String> chequeDdList = new ArrayList<String>();
private String eventGeneral = null;
private String companyGeneral = null;
List<Event> dataForGeneralReports;
public List<String> getEventIdList() {
return eventIdList;
}
public void setEventIdList(List<String> eventIdList) {
this.eventIdList = eventIdList;
}
public List<String> getEventNameList() {
return eventNameList;
}
public void setEventNameList(List<String> eventNameList) {
this.eventNameList = eventNameList;
}
public List<String> getEventVenueList() {
return eventVenueList;
}
public void setEventVenueList(List<String> eventVenueList) {
this.eventVenueList = eventVenueList;
}
public List<String> getEventTimeList() {
return eventTimeList;
}
public void setEventTimeList(List<String> eventTimeList) {
this.eventTimeList = eventTimeList;
}
public List<String> getCompanyNameList() {
return companyNameList;
}
public void setCompanyNameList(List<String> companyNameList) {
this.companyNameList = companyNameList;
}
public List<String> getTotalAmountList() {
return totalAmountList;
}
public void setTotalAmountList(List<String> totalAmountList) {
this.totalAmountList = totalAmountList;
}
public List<String> getReceivedAmountList() {
return receivedAmountList;
}
public void setReceivedAmountList(List<String> receivedAmountList) {
this.receivedAmountList = receivedAmountList;
}
public List<String> getBalanceAmountList() {
return balanceAmountList;
}
public void setBalanceAmountList(List<String> balanceAmountList) {
this.balanceAmountList = balanceAmountList;
}
public List<String> getEventTdsList() {
return eventTdsList;
}
public void setEventTdsList(List<String> eventTdsList) {
this.eventTdsList = eventTdsList;
}
public List<String> getPaymentDateList() {
return paymentDateList;
}
public void setPaymentDateList(List<String> paymentDateList) {
this.paymentDateList = paymentDateList;
}
public List<String> getChequeDdList() {
return chequeDdList;
}
public void setChequeDdList(List<String> chequeDdList) {
this.chequeDdList = chequeDdList;
}
public SessionMap<String, Object> getSessionMapGeneral() {
return sessionMapGeneral;
}
public void setSessionMapGeneral(
SessionMap<String, Object> sessionMapGeneral) {
this.sessionMapGeneral = sessionMapGeneral;
}
public String getEventGeneral() {
return eventGeneral;
}
public void setEventGeneral(String eventGeneral) {
this.eventGeneral = eventGeneral;
}
public String getCompanyGeneral() {
return companyGeneral;
}
public void setCompanyGeneral(String companyGeneral) {
this.companyGeneral = companyGeneral;
}
public List<Event> getDataForGeneralReports() {
return dataForGeneralReports;
}
public void setDataForGeneralReports(List<Event> dataForGeneralReports) {
this.dataForGeneralReports = dataForGeneralReports;
}
public List<String> getEventsGeneral() {
return eventsGeneral;
}
public void setEventsGeneral(List<String> eventsGeneral) {
this.eventsGeneral = eventsGeneral;
}
public List<String> getCompaniesGeneral() {
return companiesGeneral;
}
public void setCompaniesGeneral(List<String> companiesGeneral) {
this.companiesGeneral = companiesGeneral;
}
public DataForGeneralReportsAction() {
// TODO Auto-generated constructor stub
}
#Override
public void prepare() throws Exception {
// TODO Auto-generated method stub
Connection con = null;
try {
con = new Database().Get_Connection();
// load companies
PreparedStatement ps = con
.prepareStatement("SELECT DISTINCT company_name FROM event");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
companiesGeneral.add(rs.getString("company_name"));
}
// load events
ps = con.prepareStatement("SELECT DISTINCT event_name FROM event");
rs = ps.executeQuery();
while (rs.next()) {
eventsGeneral.add(rs.getString("event_name"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
con.close();
}
}
#Override
public String execute() throws Exception {
Connection con = null;
try {
con = new Database().Get_Connection();
// load the table. The first time the table is loaded completely
String sql = "SELECT EVENT_ID, EVENT_NAME, COMPANY_NAME,EVENT_VENUE,TOTAL_AMOUNT,RECEIVED_AMOUNT,EVENT_TDS,BALANCE_AMOUNT,CHEQUE_DD_NO,"
+ "date_format(PAYMENT_DATE,'%d/%m/%Y') as dateAsPayment,EVENT_TIME "
+ "FROM event";
String where = "";
// if instead this action has been called from the JSP page,
// the result is filtered on event and company:
if (eventGeneral != null && companyGeneral != null) {
where = " WHERE event_name = ? AND company_name = ?";
}
// load companies
PreparedStatement ps = con.prepareStatement(sql + where);
if (where.length() > 0) {
ps.setString(1, eventGeneral);
ps.setString(2, companyGeneral);
}
dataForGeneralReports = new ArrayList<Event>();
ResultSet rs = ps.executeQuery();
int i, j = 0;
while (rs.next()) {
dataForGeneralReports.add(new Event(rs.getString("EVENT_ID"),
rs.getString("EVENT_NAME"), rs
.getString("COMPANY_NAME"), rs
.getString("EVENT_VENUE"), rs
.getString("EVENT_TIME"), rs
.getString("TOTAL_AMOUNT"), rs
.getString("RECEIVED_AMOUNT"), rs
.getString("CHEQUE_DD_NO"), rs
.getString("dateAsPayment"), rs
.getString("BALANCE_AMOUNT"), rs
.getString("EVENT_TDS")));
eventIdList.add(rs.getString("EVENT_ID"));
eventNameList.add(rs.getString("EVENT_NAME"));
companyNameList.add(rs.getString("COMPANY_NAME"));
eventVenueList.add(rs.getString("EVENT_VENUE"));
eventTimeList.add(rs.getString("EVENT_TIME"));
totalAmountList.add(rs.getString("TOTAL_AMOUNT"));
receivedAmountList.add(rs.getString("RECEIVED_AMOUNT"));
chequeDdList.add(rs.getString("CHEQUE_DD_NO"));
paymentDateList.add(rs.getString("dateAsPayment"));
eventTdsList.add(rs.getString("EVENT_TDS"));
balanceAmountList.add(rs.getString("BALANCE_AMOUNT"));
}
sessionMapGeneral.put("eventIdPdf", eventIdList);
sessionMapGeneral.put("eventNamePdf", eventNameList);
sessionMapGeneral.put("companyNamePdf", companyNameList);
sessionMapGeneral.put("eventVenuePdf", eventVenueList);
sessionMapGeneral.put("eventTimePdf", eventTimeList);
sessionMapGeneral.put("totalAmountPdf", totalAmountList);
sessionMapGeneral.put("receivedAmountPdf", receivedAmountList);
sessionMapGeneral.put("chequeDdPdf", chequeDdList);
sessionMapGeneral.put("paymentDatePdf", paymentDateList);
sessionMapGeneral.put("eventTdsPdf", eventTdsList);
sessionMapGeneral.put("balanceAmountPdf", balanceAmountList);
} catch (Exception e) {
e.printStackTrace();
} finally {
con.close();
}
return SUCCESS;
}
public String generatePdfGeneral() throws Exception {
System.out.println(sessionMapGeneral.get("eventIdPdf"));
Document document = new Document(PageSize.A4_LANDSCAPE, 50, 50, 50, 50);
float[] columnWidths = { 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("D:\\GeneralReports.pdf"));
PdfPTable table = new PdfPTable(11);
table.setSpacingBefore(25);
table.setWidthPercentage(100);
table.setSpacingAfter(25);
table.setWidths(columnWidths);
PdfPCell c1 = new PdfPCell(new Phrase("Event ID "));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Event Name "));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Event Time"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Event Venue"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Company Name"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Total Amount"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Received Amount"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Cheque/DD Number"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Payment Date"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Event TDS"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Balance Amount"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
table.setHeaderRows(1);
PdfPCell cell = new PdfPCell();
List<String> list = (List<String>) sessionMapGeneral.get("eventIdPdf");
for (String item : list) {
cell.addElement(new Paragraph(item));
}
PdfPCell cell1 = new PdfPCell();
List<String> list1 = (List<String>) sessionMapGeneral
.get("eventNamePdf");
for (String item : list1) {
cell1.addElement(new Paragraph(item));
}
table.addCell(cell1);
PdfPCell cell2 = new PdfPCell();
List<String> list2 = (List<String>) sessionMapGeneral
.get("eventTimePdf");
for (String item : list2) {
cell2.addElement(new Paragraph(item));
}
table.addCell(cell2);
PdfPCell cell3 = new PdfPCell();
List<String> list3 = (List<String>) sessionMapGeneral
.get("eventVenuePdf");
for (String item : list1) {
cell3.addElement(new Paragraph(item));
}
table.addCell(cell3);
PdfPCell cell4 = new PdfPCell();
List<String> list4 = (List<String>) sessionMapGeneral.get("eventIdPdf");
for (String item : list4) {
cell4.addElement(new Paragraph(item));
}
table.addCell(cell4);
PdfPCell cell5 = new PdfPCell();
List<String> list5 = (List<String>) sessionMapGeneral
.get("companyNamePdf");
for (String item : list5) {
cell5.addElement(new Paragraph(item));
}
table.addCell(cell5);
PdfPCell cell6 = new PdfPCell();
List<String> list6 = (List<String>) sessionMapGeneral
.get("totalAmountPdf");
for (String item : list6) {
cell6.addElement(new Paragraph(item));
}
table.addCell(cell6);
PdfPCell cell7 = new PdfPCell();
List<String> list7 = (List<String>) sessionMapGeneral
.get("receivedAmountPdf");
for (String item : list7) {
cell7.addElement(new Paragraph(item));
}
table.addCell(cell7);
PdfPCell cell8 = new PdfPCell();
List<String> list8 = (List<String>) sessionMapGeneral
.get("chequeDdPdf");
for (String item : list8) {
cell8.addElement(new Paragraph(item));
}
table.addCell(cell8);
PdfPCell cell9 = new PdfPCell();
List<String> list9 = (List<String>) sessionMapGeneral
.get("paymentDatePdf");
for (String item : list9) {
cell9.addElement(new Paragraph(item));
}
table.addCell(cell9);
PdfPCell cell10 = new PdfPCell();
List<String> list10 = (List<String>) sessionMapGeneral
.get("eventTdsPdf");
for (String item : list10) {
cell10.addElement(new Paragraph(item));
}
table.addCell(cell10);
PdfPCell cell11 = new PdfPCell();
List<String> list11 = (List<String>) sessionMapGeneral
.get("balanceAmountPdf");
for (String item : list11) {
cell11.addElement(new Paragraph(item));
}
table.addCell(cell11);
document.open();
document.add(table);
document.close();
return "success";
}
public String generateGeneralXls() throws Exception {
try {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("FirstSheet");
HSSFRow rowhead = sheet.createRow((short) 0);
rowhead.createCell(0).setCellValue("Event ID");
rowhead.createCell(1).setCellValue("Event Name");
rowhead.createCell(2).setCellValue("Event Time");
rowhead.createCell(3).setCellValue("Event Venue");
rowhead.createCell(4).setCellValue("Company Name");
rowhead.createCell(5).setCellValue("Total Amount");
rowhead.createCell(6).setCellValue("Received Amount");
rowhead.createCell(7).setCellValue("Payment Date");
rowhead.createCell(8).setCellValue("Cheque/DD No.");
rowhead.createCell(9).setCellValue("Event TDS");
rowhead.createCell(10).setCellValue("Balance Amount");
FileOutputStream fileOut;
fileOut = new FileOutputStream("D:\\Samplmgjkm.xls");
// HSSFRow row1 = sheet.createRow((short) 1);
System.out.println(sessionMapGeneral.size());
for (int i = 1; i <= sessionMapGeneral.size(); i++) {
HSSFRow row1 = sheet.createRow((short) i);
row1.createCell(i-1).setCellValue(
sessionMapGeneral.get("eventIdPdf").toString());
row1.createCell(i).setCellValue(
sessionMapGeneral.get("eventNamePdf").toString());
}
/*
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
*/
workbook.write(fileOut);
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
#Override
public void setSession(Map<String, Object> map) {
// TODO Auto-generated method stub
sessionMapGeneral = (SessionMap) map;
}
}
I have edited my code where I get result in string but all records are displayed in single cell. I want each record in new cell.I have attached image of how it looks.
No error is displayed. Please help me to solve my problem.
Try this:
FileOutputStream fileOut;
fileOut = new FileOutputStream("D:\\Samplmgjkm.xls");
int nextRow = 1;
while(rs.next()){
HSSFRow r = sheet.getRow(nextRow);
if (r == null) {
r = sheet.createRow(nextRow);
}
HSSFCell c = r.getCell(1, Row.CREATE_NULL_AS_BLANK);
c.setCellValue(rs.getString(1));
HSSFCell c2 = r.getCell(2, Row.CREATE_NULL_AS_BLANK);
c2.setCellValue(rs.getString(2));
nextRow++;
}
First poposal : use event object.You build a list of Event object you could use and put in SessionMap and if you want, build the other list using list of Events)
here and Exemple assuming you have getter in Event Object :
public String generateGeneralXls() throws Exception {
try {
String titles="Event ID,Event Name,Event Time,Event Venue,Company Name,Total Amount,Received Amount,Payment Date,Cheque/DD No,Event TDS,Balance Amount";
String[]arrTiltes=titles.split(",");
FileOutputStream fileOut = new FileOutputStream("poi-test.xls");
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet worksheet = workbook.createSheet("POI Worksheet");
int row=0;
HSSFRow rowTitle = worksheet.createRow(row);
// set titles
for(int i=0;i<arrTiltes.length;i++){
HSSFCell cellTitle=rowTitle.createCell(i);
cellTitle.setCellValue(arrTiltes[i]);
}
//setting values
row++;
for(Event dataValue:dataForGeneralReports){
HSSFRow rowValue = worksheet.createRow(row);
HSSFCell cell0 = rowValue.createCell(0);
cell0.setCellValue(dataValue.getEventID());
HSSFCell cell1 = rowValue.createCell(1);
cell1.setCellValue(dataValue.getEventName());
HSSFCell cell2 = rowValue.createCell(2);
cell2.setCellValue(dataValue.getEventTime());
HSSFCell cell3 = rowValue.createCell(3);
cell3.setCellValue(dataValue.getEventVenue());
HSSFCell cell4 = rowValue.createCell(4);
cell4.setCellValue(dataValue.getCompanyName());
HSSFCell cell5 = rowValue.createCell(5);
cell5.setCellValue(dataValue.getTotalAmount());
HSSFCell cell6 = rowValue.createCell(6);
cell6.setCellValue(dataValue.getReceivedAmount());
HSSFCell cell7 = rowValue.createCell(7);
cell7.setCellValue(dataValue.getPaymentDate());
HSSFCell cell8 = rowValue.createCell(8);
cell8.setCellValue(dataValue.getChequeDDNo());
HSSFCell cell9 = rowValue.createCell(9);
cell9.setCellValue(dataValue.getEventTDS());
HSSFCell cell10 = rowValue.createCell(10);
cell10.setCellValue(dataValue.getBalanceAmount());
row++;
}
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
you can use the same idea for you PDF.
be carreful in your code for PDF, i suspect you don't have results expected.
you have forgotten adding cell on table ( first cell on your code)
you have 12 cells however you declare 11:
i sugest you o remove "block" for
PdfPCell cell4
second proposal : if you want really use your lists, then iterate over each list to get first value in each of them, second value of each of them etc..
Exemple assuming all list have the same size (coming from your resultset):
public String generateGeneralXlsOther() throws Exception {
try {
String titles="eventIdPdf,eventNamePdf,companyNamePdf,eventVenuePdf,eventTimePdf,totalAmountPdf,receivedAmountPdf,receivedAmountPdf,chequeDdPdf,paymentDatePdf,eventTdsPdf,balanceAmountPdf";
String[]arrTiltes=titles.split(",");
FileOutputStream fileOut = new FileOutputStream("poi-testOtehr.xls");
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet worksheet = workbook.createSheet("POI Worksheet");
HSSFRow rowTitle = worksheet.createRow(0);
// set titles
for(int i=0;i<arrTiltes.length;i++){
HSSFCell cellTitle=rowTitle.createCell(i);
cellTitle.setCellValue(arrTiltes[i]);
}
int size=((List<String>) sessionMapGeneral.get(arrTiltes[0])).size();
for(int row=0;row<size;row++){
HSSFRow rowValue = worksheet.createRow(row+1);
int cell=0;
for(int i=0;i<arrTiltes.length;i++){
List<String> theList=(List<String>) sessionMapGeneral.get(arrTiltes[i]);
HSSFCell cell0 = rowValue.createCell(cell);
cell0.setCellValue(theList.get(row));
cell++;
}
}
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
I have checked your code and done some editing as below, so please refer below code and you will get solution.
List<String> eventIdPdf = (List<String>) sessionMapGeneral.get("eventNamePdf");
List<String> eventNamePdf = (List<String>) sessionMapGeneral.get("eventNamePdf");
for (int i = 1; i <= sessionMapGeneral.size(); i++) {
HSSFRow row1 = sheet.createRow((short) i);
row1.createCell(i-1).setCellValue(eventIdPdf.get(i).toString());
row1.createCell(i).setCellValue(eventNamePdf.get(i).toString());
}
As you are taking list of strings in one variable like eventIdPdf, so as per your use in code you are writing entire list in one cell therefor you are getting entire list in one cell, now check above method so it will display one by one answer.
Hope this will work..
Your first problem is here:
row1.createCell(i-1).setCellValue(
sessionMapGeneral.get("eventIdPdf").toString());
because sessionMapGeneral.get("eventIdPdf").toString() returns the whole eventIdList, or more precisely, all values of eventIdList as a String, so the result of this expression looks like "[value1, value2, ... , valueN]"
and this (very long) result goes comletely to the row1.createCell(i-1). But actually, Cell(i-1) should get only the "value1". And here we go to
your second problem:
for (int i = 1; i <= sessionMapGeneral.size(); i++)
You iterate just through the sessionMapGeneral, but iterating through 2D-structures like tables or matrices requires two loops (nested loop). Notice, your sessionMapGeneral's elements are Key-Value pairs, where every Key is a String, e.g. "eventIdPdf", and every Value is a List, e.g. eventIdList. So, in the first loop you should access the values (Lists) of the sessionMapGeneral to get to your data stored there. Then, in the second loop, you can iterate through the Lists you got in the first loop, in order to get every single data item and set it into the required cell of the table.
The next small problem is, you can't access SessionMap's elements by index, but there is a way to "beat the system" :) - here it is
your solution: (please pay attention to comments)
//modify your key-Strings in order to be able to sort them later easily
sessionMapGeneral.put("01_eventIdPdf", eventIdList);
sessionMapGeneral.put("02_eventNamePdf", eventNameList);
sessionMapGeneral.put("03_companyNamePdf", companyNameList);
sessionMapGeneral.put("04_eventVenuePdf", eventVenueList);
sessionMapGeneral.put("05_eventTimePdf", eventTimeList);
sessionMapGeneral.put("06_totalAmountPdf", totalAmountList);
sessionMapGeneral.put("07_receivedAmountPdf", receivedAmountList);
sessionMapGeneral.put("08_chequeDdPdf", chequeDdList);
sessionMapGeneral.put("09_paymentDatePdf", paymentDateList);
sessionMapGeneral.put("10_eventTdsPdf", eventTdsList);
sessionMapGeneral.put("11_balanceAmountPdf", balanceAmountList);
...
fileOut = new FileOutputStream("D:\\Samplmgjkm.xls");
// get all the key-Strings from sessionMapGeneral and store them in an ArrayList in order to be able to access the elements by index
List<String> keys = new ArrayList<String>(sessionMapGeneral.keySet());
// and sort them, so that they suit to your "rowhead"
Collections.sort(keys);
// iterate through the keys and fill out the xls table column by column:
for (int key = 0; key < keys.size(); key++) {
//hold the current List from sessionMapGeneral for the current column in a variable
List<String> currentList = (List<String>) (sessionMapGeneral.get(keys.get(key)));
// iterate through the current ArrayList (column) and fill out the cells of this column row by row:
for (int row = 0; row < currentList.size(); row++) {
// hold the current row in a variable
HSSFRow currentRow = sheet.getRow(row+1);
// create a row only once to avoid deleting the data set earlier and set the value into the cell
if (currentRow == null) {
currentRow = sheet.createRow((short) row+1); // I don't understand why do you cast it to short. To save 16 bit per row?
currentRow.createCell(key).setCellValue(currentList.get(row));
} else {
currentRow.createCell(key).setCellValue(currentList.get(row));
}
}
// to make the width of columns looking well, add this line here:
sheet.autoSizeColumn(key);
}

How to split a excel file into multiple files based on row count using apache poi?

I am new to apache poi, I wanted to split a excel file into multiple files based on row count.
E.g data.xlsx has 15k rows, new files should be like data_1.xlsx with 5k rows,data_2.xlsx should be 5-10k and data_3.xlsx should be 10-15k.
I've got you.
package com.industries.seanimus;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReportSplitter {
private final String fileName;
private final int maxRows;
public ReportSplitter(String fileName, final int maxRows) {
ZipSecureFile.setMinInflateRatio(0);
this.fileName = fileName;
this.maxRows = maxRows;
try {
/* Read in the original Excel file. */
OPCPackage pkg = OPCPackage.open(new File(fileName));
XSSFWorkbook workbook = new XSSFWorkbook(pkg);
XSSFSheet sheet = workbook.getSheetAt(0);
/* Only split if there are more rows than the desired amount. */
if (sheet.getPhysicalNumberOfRows() >= maxRows) {
List<SXSSFWorkbook> wbs = splitWorkbook(workbook);
writeWorkBooks(wbs);
}
pkg.close();
}
catch (EncryptedDocumentException | IOException | InvalidFormatException e) {
e.printStackTrace();
}
}
private List<SXSSFWorkbook> splitWorkbook(XSSFWorkbook workbook) {
List<SXSSFWorkbook> workbooks = new ArrayList<SXSSFWorkbook>();
SXSSFWorkbook wb = new SXSSFWorkbook();
SXSSFSheet sh = wb.createSheet();
SXSSFRow newRow;
SXSSFCell newCell;
int rowCount = 0;
int colCount = 0;
XSSFSheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
newRow = sh.createRow(rowCount++);
/* Time to create a new workbook? */
if (rowCount == maxRows) {
workbooks.add(wb);
wb = new SXSSFWorkbook();
sh = wb.createSheet();
rowCount = 0;
}
for (Cell cell : row) {
newCell = newRow.createCell(colCount++);
newCell = setValue(newCell, cell);
CellStyle newStyle = wb.createCellStyle();
newStyle.cloneStyleFrom(cell.getCellStyle());
newCell.setCellStyle(newStyle);
}
colCount = 0;
}
/* Only add the last workbook if it has content */
if (wb.getSheetAt(0).getPhysicalNumberOfRows() > 0) {
workbooks.add(wb);
}
return workbooks;
}
/*
* Grabbing cell contents can be tricky. We first need to determine what
* type of cell it is.
*/
private SXSSFCell setValue(SXSSFCell newCell, Cell cell) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
newCell.setCellValue(cell.getRichStringCellValue().getString());
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
newCell.setCellValue(cell.getDateCellValue());
} else {
newCell.setCellValue(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
newCell.setCellValue(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
newCell.setCellFormula(cell.getCellFormula());
break;
default:
System.out.println("Could not determine cell type");
}
return newCell;
}
/* Write all the workbooks to disk. */
private void writeWorkBooks(List<SXSSFWorkbook> wbs) {
FileOutputStream out;
try {
for (int i = 0; i < wbs.size(); i++) {
String newFileName = fileName.substring(0, fileName.length() - 5);
out = new FileOutputStream(new File(newFileName + "_" + (i + 1) + ".xlsx"));
wbs.get(i).write(out);
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
/* This will create a new workbook every 1000 rows. */
new ReportSplitter("Data.xlsx", 1000);
}
}
A few notes:
For writing the workbooks, I use
SXSSFWorkbook. It's a lot
faster than HSSF or XSSF, as it doesn't hold everything in memory
before writing (which causes a horrible gc mess).
The Busy Developer's Guide is your friend for learning Apache POI ;)
ENJOY!
EDIT: I've updated the code to copy cell styles as well. Two things to note about this:
Copying styles will SLOW things down considerably.
POI creates a template file that may become too big to be uncompressed, throwing a Zip bomb detected error. You can fix this by changing the minimum inflation ratio via ZipSecureFile.setMinInflateRatio(0).
Thanks for your code. Just two cent from my side
The code above does not copy the time Hence I modified it for having Time Columns which is a small modification in setValue Code.
Basically I'm checking using format part if it is a time column for which the year would be 1899
Hope it helps :)
private static SXSSFCell setValue(SXSSFCell newCell, Cell cell) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
newCell.setCellValue(cell.getRichStringCellValue().getString());
break;
case Cell.CELL_TYPE_NUMERIC:
//System.out.println("The Cell Type is numeric ");
if (DateUtil.isCellDateFormatted(cell)) {
System.out.println(cell.getDateCellValue());
Date c = cell.getDateCellValue();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm:ss");
SimpleDateFormat year = new SimpleDateFormat("yyyy");
String strTime = simpleDateFormat.format(c);
String strYear=year.format(c);
if(strYear.equals("1899"))
{
System.out.println(strTime);
newCell.setCellValue(DateUtil.convertTime(strTime));
}
else
{
newCell.setCellValue(c);
}
} else {
newCell.setCellValue(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
newCell.setCellValue(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
newCell.setCellFormula(cell.getCellFormula());
break;
default:
System.out.println("Could not determine cell type");
}
return newCell;
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class SplitFile {
private final String fileName;
private final int maxRows;
private final String path;
private final String userfilename="";
public static int filecount;
public static String taskname;
public static int rowcounter;
public SplitFile(String fileName, final int maxRows, String filepath, String userfilename) throws FileNotFoundException {
path = filepath;
taskname = userfilename;
this.fileName = fileName;
this.maxRows = maxRows;
System.out.println("In Constructor");
File file = new File(fileName);
FileInputStream inputStream = new FileInputStream(file);
try {
/* Read in the original Excel file. */
//OPCPackage pkg = OPCPackage.open(new File(fileName));
Workbook workbook = new XSSFWorkbook(inputStream);
XSSFSheet sheet = (XSSFSheet) workbook.getSheetAt(0);
System.out.println("Got Sheet");
/* Only split if there are more rows than the desired amount. */
if (sheet.getPhysicalNumberOfRows() >= maxRows) {
List<SXSSFWorkbook> wbs = splitWorkbook(workbook);
writeWorkBooks(wbs);
}
}
catch (EncryptedDocumentException | IOException e) {
e.printStackTrace();
}
}
private List<SXSSFWorkbook> splitWorkbook(Workbook workbook) {
List<SXSSFWorkbook> workbooks = new ArrayList<SXSSFWorkbook>();
SXSSFWorkbook wb = new SXSSFWorkbook();
SXSSFSheet sh = (SXSSFSheet) wb.createSheet();
SXSSFRow newRow,headRow = null;
SXSSFCell newCell;
String headCellarr[] = new String[50];
int rowCount = 0;
int colCount = 0;
int headflag = 0;
int rcountflag = 0;
int cols = 0;
XSSFSheet sheet = (XSSFSheet) workbook.getSheetAt(0);
//sheet.createFreezePane(0, 1);
int i = 0;
rowcounter++;
for (Row row : sheet) {
if(i==0)
{
//headRow = sh.createRow(rowCount++);
/* Time to create a new workbook? */
int j = 0;
for (Cell cell : row) {
//newCell = headRow.createCell(colCount++);
headCellarr[j] = cell.toString();
j++;
}
cols = j;
colCount = 0;
i++;
}
else
{
break;
}
}
for (Row row : sheet) {
//newRow = sh.createRow(rowCount++);
/* Time to create a new workbook? */
if (rowCount == maxRows) {
headflag = 1;
workbooks.add(wb);
wb = new SXSSFWorkbook();
sh = (SXSSFSheet) wb.createSheet();
rowCount = 0;
}
if(headflag == 1)
{
newRow = (SXSSFRow) sh.createRow(rowCount++);
headflag = 0;
for(int k=0;k<cols;k++)
{
newCell = (SXSSFCell) newRow.createCell(colCount++);
newCell.setCellValue(headCellarr[k]);
}
colCount = 0;
newRow = (SXSSFRow) sh.createRow(rowCount++);
for (Cell cell : row) {
newCell = (SXSSFCell) newRow.createCell(colCount++);
if(cell.getCellType() == Cell.CELL_TYPE_BLANK)
{
newCell.setCellValue("-");
}
else
{
newCell = setValue(newCell, cell);
}
}
colCount = 0;
}
else
{
rowcounter++;
newRow = (SXSSFRow) sh.createRow(rowCount++);
for(int cn=0; cn<row.getLastCellNum(); cn++) {
// If the cell is missing from the file, generate a blank one
// (Works by specifying a MissingCellPolicy)
Cell cell = row.getCell(cn, Row.CREATE_NULL_AS_BLANK);
// Print the cell for debugging
//System.out.println("CELL: " + cn + " --> " + cell.toString());
newCell = (SXSSFCell) newRow.createCell(cn);
if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
{
newCell.setCellValue(cell.getNumericCellValue());
}
else
{
newCell.setCellValue(cell.toString());
}
}
}
}
/* Only add the last workbook if it has content */
if (wb.getSheetAt(0).getPhysicalNumberOfRows() > 0) {
workbooks.add(wb);
}
return workbooks;
}
/*
* Grabbing cell contents can be tricky. We first need to determine what
* type of cell it is.
*/
private SXSSFCell setValue(SXSSFCell newCell, Cell cell) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
newCell.setCellValue(cell.getRichStringCellValue().getString());
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
newCell.setCellValue(cell.getDateCellValue());
} else {
//newCell.setCellValue(cell.getNumericCellValue());
newCell.setCellValue(cell.toString());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
newCell.setCellValue(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
newCell.setCellFormula(cell.getCellFormula());
break;
case Cell.CELL_TYPE_BLANK:
newCell.setCellValue("-");
break;
default:
System.out.println("Could not determine cell type");
newCell.setCellValue(cell.toString());
}
return newCell;
}
/* Write all the workbooks to disk. */
private void writeWorkBooks(List<SXSSFWorkbook> wbs) {
FileOutputStream out;
boolean mdir = new File(path + "/split").mkdir();
try {
for (int i = 0; i < wbs.size(); i++) {
String newFileName = fileName.substring(0, fileName.length() - 5);
//out = new FileOutputStream(new File(newFileName + "_" + (i + 1) + ".xlsx"));
out = new FileOutputStream(new File(path + "/split/" + taskname + "_" + (i + 1) + ".xlsx"));
wbs.get(i).write(out);
out.close();
System.out.println("Written" + i);
filecount++;
}
System.out.println(userfilename);
} catch (IOException e) {
e.printStackTrace();
}
}
public int sendtotalrows()
{
return rowcounter;
}
public static void main(String[] args) throws FileNotFoundException{
// This will create a new workbook every 1000 rows.
// new Splitter(filename.xlsx, No of split rows, filepath, newfilename);
new SplitFile("filepath/filename.xlsx", 10000, "filepath", "newfilename"); //No of rows to split: 10 K
}
}

Categories