InputStream inp = new FileInputStream("workbook.xls");
HSSFWorkbook wb = new HSSFWorkbook(inp);
String text="";
Sheet sheet1 = wb.getSheetAt(0);
for (Row row : sheet1) {
for (Cell cell : row) {
// Do something here
ExcelExtractor extractor = new ExcelExtractor(wb);
text=extractor.getText();
System.out.print("text="+text);
}
}
sheet1.shiftRows(0,sheet1.getLastRowNum(),1);
I am not getting the result in my excel sheet after the shiftrows is called. the data stays the same. Where am I doing it wrong?
This is because you're workbook is only open for reading since it came from an inputstream
InputStream inp = new FileInputStream("workbook.xls");
To get the result you would have to write the new workbook object out to a new xsl spreadsheet. Something like...
File outWB = new File("workbook2.xls");
OutputStream out = new FileOutputStream(outWB);
wb.write(out);
out.flush();
out.close();
You could do this to the same file but you would need to inp.close() first.
Related
I tried the below code, but it's not working
File src = new File("E:\\TestData.xlsx");
FileInputStream fs = new FileInputStream(src);
XSSFWorkbook workbook = new XSSFWorkbook(fs);
XSSFSheet sheet = workbook.getSheet("test");
XSSFRow row = sheet.getRow(1);
sheet.removeRow(row);
Note: Not getting any error also
You need to save file after row deletion
FileOutputStream outputStream = new FileOutputStream(File Path);
workbook.write(outputStream);
workbook.close();
outputStream.close();
I tried the code below, but it is not working:
public static void addExternalPivot (String pathSource, String pathOutput, String NameOfTableSource) throws IOException{
//source wb
FileInputStream fis = new FileInputStream(pathSource);
XSSFWorkbook wbSrc = new XSSFWorkbook(fis);
//output wb
FileOutputStream fos = new FileOutputStream(pathOutput);
XSSFWorkbook wb = new MyXSSFWorkbook();
//create sheet for pivot
XSSFSheet sheetOut = wb.createSheet("pivot");
//get pivot from source
XSSFPivotTable pSrc = wbSrc.getPivotTables().get(0);
//parameters to create pivot
AreaReference source = wbSrc.getTable(NameOfTableSource).getArea();
CellReference position = new CellReference("A5");
Sheet dataSheet = pSrc.getDataSheet();
//create pivot
XSSFPivotTable pivotResult = sheetOut.createPivotTable(source, position, dataSheet);
//save and close
fis.close();
wbSrc.close();
wb.write(fos);
fos.close();
wb.close();
}
I can copy datasheet directly to the output wb and create pivot, but it is not working for my case. Where i have to update 250 pivots in different files.
I want to export all the data from one workbook to another using Java. I used FOR LOOP to read all the data from the input.xls file. Then I want to Export all the read data to output.xls. I don't know how to export it after reading data. I need your help guys. Thanks.
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream("C:\\input.xls"));
FileOutputStream wbML = new FileOutputStream("C:\\output.xls");
Sheet sheet1 = wb.getSheetAt(0);
for (Row row : sheet1) {
for (Cell cell : row) {
String sValue = cell.getRichStringCellValue().getString();
System.out.println(sValue);
}
}
If you just want to copy the workbook, there is no need to iterate. Just write it to a new file.
Workbook wb = new HSSFWorkbook(new FileInputStream("C:\\input.xls"));
FileOutputStream fileOut = new FileOutputStream("C:\\ouput.xls");
wb.write(fileOut);
fileOut.close();
i am trying to edit an excel sheet using apache poi,
I have a worksheet having 2 rows and 5 columns
if second cell of row 2 is null then it should add a 3rd row ,
but i am getting a class not found exception, although the UpdateXLS is present in my D drive.
Here is the code:
public class New {
public static void main(String[] args){
try{
InputStream inp = new FileInputStream("D:\\UpdateXLS.xls");
//InputStream inp = new FileInputStream("workbook.xlsx");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
Cell cell = row.getCell(2);
if (cell == null)
cell = row.createCell(3);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("a test");
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("UpdateXLS.xls");
wb.write(fileOut);
fileOut.close();
}
catch(Exception e){
e.printStackTrace();
}
}
What should i do ?
From the comment , I can suggest you that check file exits on the path specified.
Use File
File f = new File("D:\\UpdateXLS.xls");
//check whether file exists or not to avoid any further exceptions
if(f.exists()){
Workbook wb = WorkbookFactory.create(f);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
......
.......
........
}
FileNotFoundException
Signals that an attempt to open the file denoted by a specified pathname has failed.
This exception will be thrown by the FileInputStream, FileOutputStream, and RandomAccessFile constructors when a file with the specified pathname does not exist.
It will also be thrown by these constructors if the file does exist but for some reason is inaccessible, for example when an attempt is made to open a read-only file for writing.
When you are writing a file check your path
FileOutputStream fileOut = new FileOutputStream("UpdateXLS.xls");
Change to
FileOutputStream fileOut = new FileOutputStream("D:\\UpdateXLS.xls");
The problem seems to be at
FileOutputStream fileOut = new FileOutputStream("UpdateXLS.xls");
try to replace the line with:
FileOutputStream fileOut = new FileOutputStream("D:\\UpdateXLS.xls");
We have next situation: our system has data export in xls format, this is huge file with many rows and columns.
And after user download and open document he see document scrolled to last column and last Spreadsheet tab. This is very annoying, better to set focus on first tab and first cell. I did simple test code to see how it works:
public class SelectionTest {
public static String file = "/usr/test/poi.test/src/main/resources/test";
#Test
public void test() throws FileNotFoundException, IOException {
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(file));
HSSFSheet s = wb.getSheetAt(0);
s.setActive(true);
HSSFRow row = s.getRow(0);
HSSFCell cell = row.getCell(0);
cell.setAsActiveCell();
FileOutputStream out = new FileOutputStream(file);
wb.write(out);
out.close();
}
}
And this doesn't works.
Here is working solution, have found this here
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(file));
HSSFSheet s = wb.getSheetAt(0);
wb.setActiveSheet(0);
s.showInPane(0, 0);
FileOutputStream out = new FileOutputStream(file);
wb.write(out);
out.close();
This worked for me:
sheet.setActiveCell(new CellAddress(0, 0));