Hi,
We have certainly functionality that uses POI which we want to replace in a phased manner using Aspose. Till that time, we would like to use the existing code.
For merging 2 workbooks, we use Aspose.
But after merging 2 workbooks using Aspose, we are not able to delete the sheet using POI on the same workbook.
But the same POI API works fine on any normal excel workbook. Please find the sample code and sample workbook attached.
Book1.xls - Input file
outputWithAspose.xls - output excel produced by Aspose
/*
* File : $Source: $
* Version : $Revision: $
* Date : $Date: $
* Modified by : $Author: $
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.aspose.cells.FileFormatType;
import com.aspose.cells.License;
import com.aspose.cells.Workbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class TestAspose
{
static String wd = "C:\\Developer\\TEMP\\frame\\";
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception
{
File file = new File("C:\\Developer\\TEMP\\frame\\");
File[] inputFiles = file.listFiles();
License license = new License();
license.setLicense(new FileInputStream("C:\\Developer\\work\\workspace\\TestAspose\\lib\\Aspose.Cells.lic"));
Workbook outputWorkbook = new Workbook();
for (File inputFile : inputFiles)
{
if (!inputFile.getAbsolutePath().equalsIgnoreCase("C:\\Developer\\TEMP\\frame\\master_blank.xls"))
{
Workbook book = new Workbook();
book.open(inputFile.getAbsolutePath());
outputWorkbook.combine(book);
}
}
String fileName = wd + "outputWithAspose.xls";
outputWorkbook.save(fileName, FileFormatType.EXCEL97TO2003);
//
// An excel file name. You can create a file name with a full path
// information.
String filename1 = wd + "text1.xls";
FileInputStream fis = null;
FileOutputStream fos = null;
try
{
//
// Create a FileInputStream that will be use to read the excel file.
//
fis = new FileInputStream(fileName);
fos = new FileOutputStream(filename1);
//
// Create an excel workbook from the file system.
//
HSSFWorkbook workBook = new HSSFWorkbook(fis);
int controlSheetIndex = workBook.getSheetIndex("ControlSheet");
if (controlSheetIndex != -1)
{
System.out.println("control sheet found");
workBook.removeSheetAt(controlSheetIndex);
}
workBook.write(fos);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (fis != null)
{
fis.close();
}
}
}
}
Exception:
Exception in thread "main" java.lang.ClassCastException: org.apache.poi.hssf.record.BOFRecord cannot be cast to org.apache.poi.hssf.record.TabIdRecord
at org.apache.poi.hssf.model.InternalWorkbook.fixTabIdRecord(InternalWorkbook.java:782)
at org.apache.poi.hssf.model.InternalWorkbook.removeSheet(InternalWorkbook.java:752)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.removeSheetAt(HSSFWorkbook.java:827)
at poi.TestDeleteSheet.main(TestDeleteSheet.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:110)
Please help at the earliest.