Results 1 to 1 of 1
- 07-31-2008, 10:44 AM #1
Member
- Join Date
- Jul 2008
- Posts
- 2
- Rep Power
- 0
POI for delete excel record with keeping the existing macros
Hi All,
I am new to POI and HSSF.
I have already an existing excel file. In that excel there is a shhet having some data in side this and also there is some excel macro in side this excel.
I want to delete all the records of that sheet excluding the 1st rows(Header).
I used the following code for this, but it's deleting the data as well as all the macros. that means when I m opening the excel after execution of the java code , excel shows following error as pop up
an error occurred initializing the VBA libraries
Please help me as it's urgent
package com.iflex.masterLookUp;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
//import org.apache.commons.lang.StringUtils;
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.poifs.filesystem.POIFSFileSystem;
public class DeleteTest {
public static void main(String[] args){
String fileName = "D:\\kirti\\test.xls";
// String fileNameNew = "D:\\kirti\\test1.xls";
// if (StringUtils.isEmpty(fileName) || !fileName.endsWith("xls")) {
//
// throw new IllegalArgumentException("Only files with extension XLS can be read");
//
// }
try{
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fileName));
HSSFWorkbook book = new HSSFWorkbook(fs);
System.out.println("No of sheets:"+book.getNumberOfSheets());
if(book.getNumberOfSheets()>0){
HSSFSheet sheet = book.getSheetAt(0);
System.out.println("PhysicalRowNum:"+sheet.getPhys icalNumberOfRows());
System.out.println("LastRowNum:"+sheet.getLastRowN um());
int rowNo=sheet.getLastRowNum();
if(sheet.getLastRowNum()>0){
for(int i=1;i<=rowNo;i++){
HSSFRow row = sheet.getRow(i);
System.out.println("Row:"+i+"\n");
if(i>=1) {
sheet.removeRow(row);
}
}
//sheet.shiftRows(0, 3, -3);
//sheet.createRow(3);
}
}
fs = null;
FileOutputStream fos = new FileOutputStream(fileName);
book.write(fos);
fos.close();
}
catch(IOException ioe){
System.out.println("IOException raised:"+ioe);
}
catch(Exception e){
System.out.println("Other exception raised:"+e);
}
}
}
Thanks
Kirti Das
Similar Threads
-
POI - excel macros manipulation
By Jay in forum Advanced JavaReplies: 0Last Post: 07-31-2008, 10:04 AM -
need to add text field on existing dialog
By SHAshank1 in forum SWT / JFaceReplies: 0Last Post: 07-23-2008, 03:15 PM -
How to convert the existing web applicatons to Portlets
By RamaKoti in forum Web FrameworksReplies: 0Last Post: 06-18-2008, 03:35 PM -
Create a new directory in existing Zip archive
By satishbejgum in forum Advanced JavaReplies: 1Last Post: 12-23-2007, 06:05 AM -
Creating an ArrayList from an existing LinkedList
By Java Tip in forum Java TipReplies: 0Last Post: 12-05-2007, 02:09 PM
Bookmarks