java worksheet写入文件,写入现有的Excel文件

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();

}

}

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 is just an interface with only one method i.e getString() you can learn more about it here

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

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 and currentRow define the index and value contains the String to be placed in that cell.

Hope it helps

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值