package com.rearrange;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CreateExcelFromSelectedRowsAndColumns2 {
public static void main(String[] args) {
try {
// excel files
FileInputStream excellFile1 = new FileInputStream(new File(
"E:\\EclipseWorkSpace_8thApril\\ReadExcelFile\\inputExcel.xlsx"));
// input row numbers and column numbers
int[] irows = { 0, 1, 2, 3, 4 };
int[] icols = { 0, 2, 1, 3 };
// Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(excellFile1);
// Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
int count=sheet.getLastRowNum();
// add sheet2 to sheet1
XSSFWorkbook outWorkbook = getFilteredWorkBook(sheet, irows, icols);
excellFile1.close();
// save merged file
File outFile = new File("E:\\filtered1.xlsx");
if (!outFile.exists()) {
outFile.createNewFile();
}
FileOutputStream out = new FileOutputStream(outFile);
outWorkbook.write(out);
out.close();
System.out.println("Files were merged succussfully");
} catch (Exception e) {
e.printStackTrace();
}
}
private static XSSFWorkbook getFilteredWorkBook(XSSFSheet sheet,
int[] irows, int[] icols) {
// create New workbook
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet outSheet = workbook.createSheet();
Map<Integer, XSSFCellStyle> styleMap = new HashMap<Integer, XSSFCellStyle>();
int i = 0;
// get rows with given row numbers
for (int rowNum : irows) {
if (rowNum >= sheet.getFirstRowNum()
&& rowNum <= sheet.getLastRowNum()) {
// create new row
XSSFRow outRow = outSheet.createRow(i);
XSSFRow row = sheet.getRow(rowNum);
int j = 0;
// get columns with given column numbers
for (int colNum : icols) {
if (colNum >= sheet.getRow(0).getFirstCellNum()
&& colNum <= sheet.getRow(0).getLastCellNum()) {
// create new column
XSSFCell outCell = outRow.createCell(j);
XSSFCell cell = row.getCell(colNum);
if (cell != null) {
j++;
if (cell.getSheet().getWorkbook() == outCell
.getSheet().getWorkbook()) {
outCell.setCellStyle(cell.getCellStyle());
} else {
int stHashCode = cell.getCellStyle().hashCode();
XSSFCellStyle newCellStyle = styleMap
.get(stHashCode);
if (newCellStyle == null) {
newCellStyle = outCell.getSheet()
.getWorkbook().createCellStyle();
newCellStyle.cloneStyleFrom(cell
.getCellStyle());
styleMap.put(stHashCode, newCellStyle);
}
// outCell.setCellStyle(newCellStyle);
}
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
outCell.setCellFormula(cell.getCellFormula());
break;
case HSSFCell.CELL_TYPE_NUMERIC:
outCell.setCellValue(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING:
outCell.setCellValue(cell.getStringCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK:
outCell.setCellType(HSSFCell.CELL_TYPE_BLANK);
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
outCell.setCellValue(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_ERROR:
outCell.setCellErrorValue(cell
.getErrorCellValue());
break;
default:
outCell.setCellValue(cell.getStringCellValue());
break;
}
}
}
}
i++;
}
}
return workbook;
}
}
------------------------------
Get Count of rows:
package com.rearrange;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CountOfRows {
public static void main(String[] args) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook("E:\\EclipseWorkSpace_8thApril\\ReadExcelFile\\inputExcel.xlsx");
// Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
System.out.println(sheet.getLastRowNum());
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CreateExcelFromSelectedRowsAndColumns2 {
public static void main(String[] args) {
try {
// excel files
FileInputStream excellFile1 = new FileInputStream(new File(
"E:\\EclipseWorkSpace_8thApril\\ReadExcelFile\\inputExcel.xlsx"));
// input row numbers and column numbers
int[] irows = { 0, 1, 2, 3, 4 };
int[] icols = { 0, 2, 1, 3 };
// Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(excellFile1);
// Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
int count=sheet.getLastRowNum();
// add sheet2 to sheet1
XSSFWorkbook outWorkbook = getFilteredWorkBook(sheet, irows, icols);
excellFile1.close();
// save merged file
File outFile = new File("E:\\filtered1.xlsx");
if (!outFile.exists()) {
outFile.createNewFile();
}
FileOutputStream out = new FileOutputStream(outFile);
outWorkbook.write(out);
out.close();
System.out.println("Files were merged succussfully");
} catch (Exception e) {
e.printStackTrace();
}
}
private static XSSFWorkbook getFilteredWorkBook(XSSFSheet sheet,
int[] irows, int[] icols) {
// create New workbook
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet outSheet = workbook.createSheet();
Map<Integer, XSSFCellStyle> styleMap = new HashMap<Integer, XSSFCellStyle>();
int i = 0;
// get rows with given row numbers
for (int rowNum : irows) {
if (rowNum >= sheet.getFirstRowNum()
&& rowNum <= sheet.getLastRowNum()) {
// create new row
XSSFRow outRow = outSheet.createRow(i);
XSSFRow row = sheet.getRow(rowNum);
int j = 0;
// get columns with given column numbers
for (int colNum : icols) {
if (colNum >= sheet.getRow(0).getFirstCellNum()
&& colNum <= sheet.getRow(0).getLastCellNum()) {
// create new column
XSSFCell outCell = outRow.createCell(j);
XSSFCell cell = row.getCell(colNum);
if (cell != null) {
j++;
if (cell.getSheet().getWorkbook() == outCell
.getSheet().getWorkbook()) {
outCell.setCellStyle(cell.getCellStyle());
} else {
int stHashCode = cell.getCellStyle().hashCode();
XSSFCellStyle newCellStyle = styleMap
.get(stHashCode);
if (newCellStyle == null) {
newCellStyle = outCell.getSheet()
.getWorkbook().createCellStyle();
newCellStyle.cloneStyleFrom(cell
.getCellStyle());
styleMap.put(stHashCode, newCellStyle);
}
// outCell.setCellStyle(newCellStyle);
}
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
outCell.setCellFormula(cell.getCellFormula());
break;
case HSSFCell.CELL_TYPE_NUMERIC:
outCell.setCellValue(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING:
outCell.setCellValue(cell.getStringCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK:
outCell.setCellType(HSSFCell.CELL_TYPE_BLANK);
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
outCell.setCellValue(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_ERROR:
outCell.setCellErrorValue(cell
.getErrorCellValue());
break;
default:
outCell.setCellValue(cell.getStringCellValue());
break;
}
}
}
}
i++;
}
}
return workbook;
}
}
------------------------------
Get Count of rows:
package com.rearrange;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CountOfRows {
public static void main(String[] args) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook("E:\\EclipseWorkSpace_8thApril\\ReadExcelFile\\inputExcel.xlsx");
// Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
System.out.println(sheet.getLastRowNum());
}
}
No comments:
Post a Comment