我在这里看到了许多类似的问题,但是我仍然无法弄清楚为什么我的java程序出现相同的错误。
我将所有.jar文件放在类路径中,但是存在相同的错误。 我的代码:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
public class Export2Excel {
public static void main(String[] args) {
try {
//create .xls and create a worksheet.
FileOutputStream fos = new FileOutputStream("D:\\data2excel.xls");
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet worksheet = workbook.createSheet("My Worksheet");
//Create ROW-1
HSSFRow row1 = worksheet.createRow((short) 0);
//Create COL-A from ROW-1 and set data
HSSFCell cellA1 = row1.createCell((short) 0);
cellA1.setCellValue("Sno");
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFillForegroundColor(HSSFColor.GOLD.index);
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellA1.setCellStyle(cellStyle);
//Create COL-B from row-1 and set data
HSSFCell cellB1 = row1.createCell((short) 1);
cellB1.setCellValue("Name");
cellStyle = workbook.createCellStyle();
cellStyle.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index);
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellB1.setCellStyle(cellStyle);
//Create COL-C from row-1 and set data
HSSFCell cellC1 = row1.createCell((short) 2);
cellC1.setCellValue(true);
//Create COL-D from row-1 and set data
HSSFCell cellD1 = row1.createCell((short) 3);
cellD1.setCellValue(new Date());
cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat
.getBuiltinFormat("m/d/yy h:mm"));
cellD1.setCellStyle(cellStyle);
//Save the workbook in .xls file
workbook.write(fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我将这些jar文件包含在代码文件夹中; commons-codec-1.9.jar poi-3.11-beta2.jar poi-ooxml-3.11-beta2.jar poi-ooxml-schemas-3.11-beta2.jar stax-api-1.0.1.jar xmlbeans-2.6.0.jar
这些是我的命令; javac“ Export2Excel.java” java“ Export2Excel”
有人可以指出正确的方向来解决这个问题吗?