public class ExportXLSUtil {
public static void export(String filePath, Object[][] values) {
WritableWorkbook book = null;
try {
book = Workbook.createWorkbook(new File(filePath));
WritableSheet sheet = book.createSheet("Sheet_1", 0);
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < values[i].length; j++) {
String value = (String) values[i][j];
Label label = new Label(i, j, value);
sheet.addCell(label);
}
}
book.write();
} catch (IOException e) {
e.printStackTrace();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
} finally {
try {
if (book != null) {
book.close();
}
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void importExl(String filePath) {
Workbook book = null;
try {
book = Workbook.getWorkbook(new File(filePath));
Sheet sheet = book.getSheet(0);
int column = sheet.getColumns();
int rows = sheet.getRows();
for (int i = 0; i < column; i++) {
for (int j = 0; j < rows; j++) {
Cell cell = sheet.getCell(i, j);
String result = cell.getContents();
System.out.println(result);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (book != null) {
book.close();
}
}
}
}
excel导入导出
最新推荐文章于 2025-05-19 18:00:00 发布