最近做需求时需要将用户给定的excel树结构信息保存到数据库中,所以写了一个java小程序,从excel中读取数据最终生成insert语句;
说明:代码依赖poi包
1 Excel数据及数据库表结构
excel树结构信息数据为如下形式:
数据库表字段:id, parent_id,name,org_id,created_by,created_date,updated_by,updated_date
2 代码
public class TreeListConvertDemo {
public static void main(String[] args) {
buildInsertSqlFromXLSX("D:/tmp/分类树.xlsx");
}
public static void buildInsertSqlFromXLSX(String xlsxFileName) {
// String xlsxFileName = "D:/tmp/分类树.xlsx";
XSSFWorkbook workbook = null;
InputStream is = null;
try {
String lineSeparator = System.getProperty("line.separator", "\n");
is = new FileInputStream(xlsxFileName);
workbook = new XSSFWorkbook(is);
int numOfsheets = workbook.getNumberOfSheets();
for (int inde