Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-31-2008, 12:44 PM
Jay Jay is offline
Member
 
Join Date: Jul 2008
Posts: 2
Jay is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
POI - excel macros manipulation Jay Advanced Java 0 07-31-2008 12:04 PM
need to add text field on existing dialog SHAshank1 SWT / JFace 0 07-23-2008 05:15 PM
How to convert the existing web applicatons to Portlets RamaKoti Web Frameworks 0 06-18-2008 05:35 PM
Create a new directory in existing Zip archive satishbejgum Advanced Java 1 12-23-2007 08:05 AM
Creating an ArrayList from an existing LinkedList Java Tip Java Tips 0 12-05-2007 04:09 PM


All times are GMT +3. The time now is 04:23 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org