gpt4 book ai didi

java.io.FileNotFoundException : Acess Denied using jexcel

转载 作者:行者123 更新时间:2023-12-02 06:39:54 25 4
gpt4 key购买 nike

我写了这个简单的程序:

public class WriteExcel {

private WritableCellFormat timesBoldUnderline;
private WritableCellFormat times;
private String inputFile;

public void setOutputFile(String inputFile) {
this.inputFile = inputFile;
}

public void write() throws IOException, WriteException {
File file = new File(inputFile);
WorkbookSettings wbSettings = new WorkbookSettings();

wbSettings.setLocale(new Locale("en", "EN"));

WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
workbook.createSheet("Report", 0);
WritableSheet excelSheet = workbook.getSheet(0);
createLabel(excelSheet);
createContent(excelSheet);

workbook.write();
workbook.close();
}

private void createLabel(WritableSheet sheet) throws WriteException {
// Lets create a times font
WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
// Define the cell format
times = new WritableCellFormat(times10pt);
// Lets automatically wrap the cells
times.setWrap(true);

// Create create a bold font with unterlines
WritableFont times10ptBoldUnderline = new WritableFont(
WritableFont.TIMES, 10, WritableFont.BOLD, false,
UnderlineStyle.SINGLE);
timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
// Lets automatically wrap the cells
timesBoldUnderline.setWrap(true);

CellView cv = new CellView();
cv.setFormat(times);
cv.setFormat(timesBoldUnderline);
cv.setAutosize(true);

// Write a few headers
addCaption(sheet, 0, 0, "Header 1");
addCaption(sheet, 1, 0, "This is another header");

}

private void createContent(WritableSheet sheet) throws WriteException,
RowsExceededException {
// Write a few number
for (int i = 1; i < 10; i++) {
// First column
addNumber(sheet, 0, i, i + 10);
// Second column
addNumber(sheet, 1, i, i * i);
}
// Lets calculate the sum of it
StringBuffer buf = new StringBuffer();
buf.append("SUM(A2:A10)");
Formula f = new Formula(0, 10, buf.toString());
sheet.addCell(f);
buf = new StringBuffer();
buf.append("SUM(B2:B10)");
f = new Formula(1, 10, buf.toString());
sheet.addCell(f);

// Now a bit of text
for (int i = 12; i < 20; i++) {
// First column
addLabel(sheet, 0, i, "Boring text " + i);
// Second column
addLabel(sheet, 1, i, "Another text");
}
}

private void addCaption(WritableSheet sheet, int column, int row, String s)
throws RowsExceededException, WriteException {
Label label;
label = new Label(column, row, s, timesBoldUnderline);
sheet.addCell(label);
}

private void addNumber(WritableSheet sheet, int column, int row,
Integer integer) throws WriteException, RowsExceededException {
Number number;
number = new Number(column, row, integer, times);
sheet.addCell(number);
}

private void addLabel(WritableSheet sheet, int column, int row, String s)
throws WriteException, RowsExceededException {
Label label;
label = new Label(column, row, s, times);
sheet.addCell(label);
}

public static void main(String[] args) throws WriteException, IOException {
WriteExcel test = new WriteExcel();
test.setOutputFile("C:/Users/test/Desktop/Output");
test.write();
System.out.println("Please check the result file!");
}

}

当我运行该程序时,我得到:

Exception in thread "main" java.io.FileNotFoundException: C:\Users\test\Desktop\Output (Access denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:194)
at java.io.FileOutputStream.<init>(FileOutputStream.java:145)
at jxl.Workbook.createWorkbook(Workbook.java:301)
at excelProcessor.WriteExcel.write(WriteExcel.java:37)
at excelProcessor.WriteExcel.main(WriteExcel.java:126)

我的问题是无法输出文件。当我这样做时,我得到了这个异常(exception)。我正在运行带有 eclipse Juno Service Release 2JDK 1.6win 7 机器。我也尝试过以管理员身份启动 Eclipse,但是不起作用。

我真的很感谢您的回答!

更新

我的异常(exception)点在这里:

WritableWorkbook 工作簿 = Workbook.createWorkbook(file, wbSettings);

但是,不知道要改变什么......

最佳答案

尝试以下操作:

  • 确保所有类似名称的 Excel 工作簿(输出)均已关闭(避免 Excel 一次只能打开一个唯一文件名的限制)。为了获得最佳效果,只需关闭 Excel。

  • 尝试添加 .xls 扩展名:

    test.setOutputFile("C:/Users/test/Desktop/Output.xls");
  • 尝试删除正向路径并仅使用文件名:

    test.setOutputFile("Output.xls");  //should go to your project root

关于java.io.FileNotFoundException : Acess Denied using jexcel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19252856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com