我已经下载了Apache Poi Jar,但是当我编写以下代码(youtube讲师轻松运行)时,它没有给我任何输出excel文件。我在这做错了什么?我将鼠标悬停在HSSFWorkbook
eclipse上告诉我
org.apache.poi.hssf.usermodel.HSSFWorkbook Note: This element neither has attached source nor attached Javadoc and hence no Javadoc could be found.
代码。
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class WriteExcel {
public static void main(String[] args) throws FileNotFoundException, IOException {
HSSFWorkbook workbook= new HSSFWorkbook();
HSSFSheet sheet= workbook.createSheet("FirstExcelSheet");
HSSFRow row= sheet.createRow(0);
HSSFCell cell= row.createCell(0);
cell.setCellValue("1,Cell");
workbook.write(new FileOutputStream("excel.xls"));
workbook.close();
workbook.getFirstVisibleTab();
}
}
答案 0 :(得分:1)
首先使用XSSFWorkbook而不是HSSFWorkbook来看看这个link, 你需要输出这个文件的第二件事就是添加这一行
try (FileOutputStream outputStream = new FileOutputStream("slsx.xlsx")) {
workbook.write(outputStream);
所以你的代码应该是这样的
public static void main(String[] args) throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet spreadsheet = workbook.createSheet("sheet1");
XSSFRow row = spreadsheet.createRow(1);
XSSFCell cell = row.createCell(1);
cell.setCellValue("test");
try (FileOutputStream out = new FileOutputStream(new File("Writesheet.xlsx"))) {
workbook.write(out);
}
System.out.println("Writesheet.xlsx written successfully");
}
答案 1 :(得分:0)
你应该在这一行添加你的路径,如:
workbook.write(new FileOutputStream("{yourpath}/excel.xls"));
它有效..