该文将主要介绍如何在Java应用程序中使用Spire.XLS for Java 插入或删除隐藏Excel工作表。
首先,请查看示例文档:
添加工作表到Excel文档:
import com.spire.xls.*;
public class ExcelWorksheet {
public static void main(String[] args) throws Exception {
String inputFile = "Sample.xlsx";
String outputFile = "output/AddWorksheet.xlsx";
//加载示例文档
Workbook workbook = new Workbook();
workbook.loadFromFile(inputFile);
//添加新的工作表
Worksheet sheet = workbook.getWorksheets().add("AddNewSheet");
sheet.getCellRange("C5").setText("这是新加内容");
//保存文档
workbook.saveToFile(outputFile, ExcelVersion.Version2010);
}
}
效果图:
删除Excel工作表:
import com.spire.xls.*;
public class ExcelWorksheet {
public static void main(String[] args) throws Exception {
String inputFile = "Sample.xlsx";
String outputFile = "output/RemoveWorksheet.xlsx";
//加载示例文档
Workbook workbook = new Workbook();
workbook.loadFromFile(inputFile);
//删除第二个工作表
Worksheet sheet1 = workbook.getWorksheets().get(1);
sheet1.remove();
//保存文档
workbook.saveToFile(outputFile, ExcelVersion.Version2010);
}
}
删除第二个工作表后的效果图: