EXCEL表的中文标题怎么做封装啊
我没有说明白,昨晚版主提供了一个关于DAO玩转EXCEL的连接https://github.com/nutzam/nutzmore/tree/master/nutz-integration-json4excel,我看了一下,就不知中文标题EXCEL文档怎么用数据库封装?
先导入为对象列表,然后insert
来自炫酷的 NutzCN
有的表可以有的表不行
public static Workbook loadExcel(InputStream in) {
Object wb = null;
try {
try {
wb = WorkbookFactory.create(in);
} catch (Exception var5) {
try {
wb = new HSSFWorkbook(in);
} catch (Exception var4) {
wb = new XSSFWorkbook(in);
}
}
} catch (Exception var6) {
log.error("can't load inputstream for a workbook", var6);
}
return (Workbook)wb;
}
会报can't load inputstream for a workbook异常;
有些xlsx可以不代表什么呀。。。 你存xls试试呗
来自炫酷的 NutzCN
public static void main(String[] args) {
// 创建一个数据源
SimpleDataSource dataSource = new SimpleDataSource();
dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1/db?characterEncoding=utf8");
dataSource.setUsername("root");
dataSource.setPassword("root");
// 创建一个NutDao实例,在真实项目中, NutDao通常由ioc托管, 使用注入的方式获得.
Dao dao = new NutDao(dataSource);
// toExcel(dao);
fromExcel(dao);
}
public static void fromExcel(Dao dao) {
// 第一步,使用j4e解析excel文件获得数据集合
InputStream in = Files.findFileAsStream(Disks.absolute("D:\half.xlsx"));
List<Half> people = J4E.fromExcel(in, Half.class, null);
// 第二步,插入数据到数据库
dao.clear(Half.class);
dao.insert(people);
}
}
欢迎加入Nutz社区群,群聊号码:68428921
来自炫酷的 NutzCN
@Table("half_list")
@J4EName("人员")
public class Half {
@J4EName("区域")
private String area;
@J4EName("国家和地区")
private String region;
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
}
不支持xlsx,我印象中
来自炫酷的 NutzCN
与xlsx没有关系的吧
在J4E这个类里面有一个loadExcel方法,方法代码如下
try {
try {
wb = WorkbookFactory.create(in);
} catch (Exception var5) {
try {
wb = new HSSFWorkbook(in);
} catch (Exception var4) {
wb = new XSSFWorkbook(in);
}
}
} catch (Exception var6) {
log.error("can't load inputstream for a workbook", var6);
}
先创建 new HSSFWorkbook(in);来操作xls。
如果报异常再创建 new XSSFWorkbook(in);来操作xlsx。
具体报错信息是什么
来自炫酷的 NutzCN
[ERROR] 17:45:32.234 org.nutz.integration.json4excel.J4E.loadExcel(J4E.java:670) - can't load inputstream for a workbook
java.io.IOException: Stream Closed
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:255)
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at java.io.PushbackInputStream.read(PushbackInputStream.java:186)
at java.util.zip.ZipInputStream.readFully(ZipInputStream.java:403)
at java.util.zip.ZipInputStream.readLOC(ZipInputStream.java:278)
at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:122)
at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.<init>(ZipInputStreamZipEntrySource.java:51)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:83)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:267)
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:39)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:204)
at org.nutz.integration.json4excel.J4E.loadExcel(J4E.java:665)
at org.nutz.integration.json4excel.J4E.fromExcel(J4E.java:338)
at cn.wizzer.gr.api.commons.core.ExceltText1.fromExcel(ExceltText1.java:31)
at cn.wizzer.gr.api.commons.core.ExceltText1.main(ExceltText1.java:25)
Exception in thread "main" java.lang.NullPointerException
at org.nutz.integration.json4excel.J4E.getSheet(J4E.java:628)
at org.nutz.integration.json4excel.J4E.fromExcel(J4E.java:339)
at cn.wizzer.gr.api.commons.core.ExceltText1.fromExcel(ExceltText1.java:31)
at cn.wizzer.gr.api.commons.core.ExceltText1.main(ExceltText1.java:25)
Process finished with exit code 1
欢迎加入Nutz社区|NutzBoot & NutzWk,群聊号码:24457628
来自炫酷的 NutzCN