Java 构造函数XSSF工作簿(FileInputStream)未定义
我正在尝试从excel文件读取数据,我使用以下代码:Java 构造函数XSSF工作簿(FileInputStream)未定义,java,excel,apache-poi,Java,Excel,Apache Poi,我正在尝试从excel文件读取数据,我使用以下代码: File Excel = new File("C:\\Users\\data.xlsx"); FileInputStream fis = new FileInputStream(Excel); XSSFWorkbook wb = new XSSFWorkbook(fis); 但我得到的编译错误如下 构造函数XSSF工作簿(FileInputStream)未定义请尝试此构造函数 public static void
File Excel = new File("C:\\Users\\data.xlsx");
FileInputStream fis = new FileInputStream(Excel);
XSSFWorkbook wb = new XSSFWorkbook(fis);
但我得到的编译错误如下
构造函数XSSF工作簿(FileInputStream)
未定义请尝试此构造函数
public static void setExcelFile(String Path, String SheetName) throws IOException, InvalidFormatException {
try {
// Open the Excel file
FileInputStream ExcelFile = new FileInputStream(Path);
// Access the required test data sheet
ExcelWBook = new XSSFWorkbook("C:\\Users\\data.xlsx");
ExcelWSheet = ExcelWBook.getSheet(SheetName);
} catch (Exception e) {
throw (e);
}
}
这是我的工作
成功地
try (FileInputStream file = new FileInputStream(new File(filename))) {
**Workbook workbook = WorkbookFactory.create(file);**
试一试{
String filename=“C:\\Users\\NIKITA\\Documents\\NetBeansProjects\\NickPrac\\exceldatabase.xlsx”;
out.print(文件名);
try(FileInputStream文件=新FileInputStream(新文件(文件名))){
工作簿=WorkbookFactory.create(文件);
Class.forName(“com.mysql.jdbc.Driver”);
Connection con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/test“,”根“,”根“);
字符串jdbc_insert_sql=“insert INTO employee”+“value(?,,?)”;
PreparedStatement preStatement=con.prepareStatement(jdbc\u insert\u sql);
工作表=工作簿。getSheetAt(0);
行行;
System.out.println(“最后一行号为========”+sheet.getLastRowNum());
对于(int i=1;i如果您有文件
,为什么要尝试使用输入流
?嗨..我无法解决这个问题,,,您能给我发送代码片段吗?嗯,我提供的链接已经包含了从文件打开XSSF工作簿所需的代码!更好的是,使用WorkbookFactory.create打开该文件,而h将同时处理Excel 97-2003和Excel 2007+文件。WorkbookFactory.create还允许您指定是否以只读方式打开工作簿,并在工作簿加密时指定密码。这将使用非Apache POI类,可能会打开文件两次,并使用硬编码路径。。。
String filename="C:\\Users\\NIKITA\\Documents\\NetBeansProjects\\NickPrac\\exceldatabase.xlsx";
out.print(filename);
try (FileInputStream file = new FileInputStream(new File(filename))) {
Workbook workbook = WorkbookFactory.create(file);
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test" , "root" , "root");
String jdbc_insert_sql = "INSERT INTO employee"+"VALUES(?,?,?)";
PreparedStatement preStatement = con.prepareStatement(jdbc_insert_sql);
Sheet sheet = workbook.getSheetAt(0);
Row row;
System.out.println("last row number is========="+sheet.getLastRowNum());
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
row = sheet.getRow(i);
int empId = (int) (row.getCell(0).getNumericCellValue());
String empName = row.getCell(1).getStringCellValue();
String empEmail = row.getCell(2).getStringCellValue();
String sql="insert into employee "+"values('"+empId+"','"+empName+"','"+empEmail+"')";
preStatement = (PreparedStatement) con.prepareStatement(sql);
preStatement.execute();
System.out.println("Records inserted.........."+i);
}
System.out.println("");
}
}
catch (Exception e)
{
out.println("Error");
}