写入现有的Excel文件 [英] Writing to an Existing Excel File
问题描述
package jexcel.jxl.nimit;
import java.awt.Label;
import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.CellType;
import jxl.LabelCell;
import jxl.NumberCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class ExcelJxl {
/**
* @param args
* @throws IOException
* @throws BiffException
* @throws WriteException
* @throws RowsExceededException
*/
public static void main(String[] args) throws BiffException, IOException, RowsExceededException, WriteException {
// TODO Auto-generated method stub
ExcelJxl.WriteFile("D:\nimit.xls");
}
public static void WriteFile(String path) throws BiffException, IOException, RowsExceededException, WriteException{
Workbook wb=Workbook.getWorkbook(new File(path));
WritableWorkbook copy=Workbook.createWorkbook(new File("D:\temp.xls"),wb);
WritableSheet sheet = copy.getSheet(1);
WritableCell cell = sheet.getWritableCell(0,0);
String S="nimit";
if (cell.getType() == CellType.LABEL)
{
LabelCell l = (LabelCell) cell;
l.setString(S);
}
copy.write();
copy.close();
wb.close();
}
}
我编辑了我的程序,现在它说setString()方法setString(String)未定义类型LabelCell 我读了文档,LabelCell类型中有一个方法setString。
I have edited my program, and now it says that setString() The method setString(String) is undefined for the type LabelCell I read the Documentation, there is a method setString in the LabelCell type.
推荐答案
LabelCell只是一个只有一个方法的接口,即 getString()
你可以了解更多关于它的信息这里
LabelCell is just an interface with only one method i.e getString()
you can learn more about it here
你应该使用 jxl.write.Label
。
你应该做的事情如下:
你应该导入以下文件
You should use jxl.write.Label
instead.
What you should exactly do is as follows
You should import the following file
import jxl.write.Label
以下是将所需位置的单元格添加到Excel文件的代码
Then following is the code for adding a cell at desired location to an excel file
Workbook existingWorkbook = Workbook.getWorkbook(new File(fileToEdit.getAbsolutePath()));
WritableWorkbook workbookCopy = Workbook.createWorkbook(new File("output.xls"), existingWorkbook);
WritableSheet sheetToEdit = workbookCopy.getSheet(sheetName);
WritableCell cell;
Label l = new Label(currentColumn, currentRow, value);
cell = (WritableCell) l;
sheetToEdit.addCell(cell);
workbookCopy.write();
workbookCopy.close();
existingWorkbook.close();
currentColumn
和 currentRow
定义索引和值包含要放在该单元格中的String。
currentColumn
and currentRow
define the index and value contains the String to be placed in that cell.
希望有帮助
这篇关于写入现有的Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!