问题描述
- java如何将html内容转换成xls或是xlsx文件?
-
java如何将html内容转换成xls或是xlsx文件。
目前html中分为左右两边内容,左边是各种链接,右边是一个list列表,我需要将list列表导出到excel中去。希望高手给予帮助,提供代码,不胜感激
解决方案
你的html里面的list是写死的?还是动态的?如果是动态的从数据库里面取的的话,你可以在取数据的时候直接转换,如果是写死的话,你直接写死在生成时候的
代码里面就可以了。
推荐使用poi,参考:http://zc985552943.iteye.com/blog/1491546
解决方案二:
楼主可以看看第三方的转excel类库 例如poi 之类的 应该有你需要的解决方案
解决方案三:
我这里是个导出的列子应该可以借鉴一下,获取你应该懂的。
还是直接把所以代码给你看看吧。我这里有三种格式的:
private String table;//获得表名
private String[] tableHead;//表头数据
private List objList;//获得表数据
private Field[] ziduan;//获得属性值
private String[] key_names;//从页面获取属性名
private void getData(){
key_names = cpkey.getPrimary_keys();//获得页面上选中的字段
this.table = cpkey.getPrimary_key_table();//获得表名
List list = sdfbiz.queryAllTable(table);//获得表数据
this.objList = new ArrayList();
for (int i = 0; i < list.size(); i++) {
objList.add(list.get(i));//获得包名类名
}
//获取表头数据
this.tableHead = new String[key_names.length];
for (int i = 0; i < key_names.length; i++) {
Config_Primary_Key c = new Config_Primary_Key();
c.setPrimary_key(key_names[i]);
c.setPrimary_key_table(table);
tableHead[i] = sdfbiz.selRowName(c);
}
this.ziduan = new Field[key_names.length];//获得该类的所有属性
for (Object obj : objList) {
for (int i = 0; i < key_names.length; i++) {
try {
ziduan[i] = obj.getClass().getDeclaredField(key_names[i]);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
//=============================导出excel表===========================================
public String excelprint()throws Exception{
this.getData();
//设置响应方式
response.setContentType("application/vnd.ms-excel;charset=utf-8");
String name = URLEncoder.encode(table+"数据报表.xls", "UTF-8");
response.setHeader("Content-Disposition", "attachment;filename="+name);
//创建一个excel文档
WritableWorkbook book = Workbook.createWorkbook(response.getOutputStream());
//创建sheet工作表
WritableSheet sheet = book.createSheet("报表", 0);
//创建表头
for (int i = 0; i < tableHead.length; i++) {
Label label = new Label(i,0,tableHead[i]);
sheet.addCell(label);
}
//添加表格体
for (int i = 0; i < objList.size(); i++) {
for (int j = 0; j < ziduan.length; j++) {
ziduan[j].setAccessible(true);
sheet.addCell(new Label(j,i+1,ziduan[j].get(objList.get(i))+""));
}
}
book.write();//写execel文档
book.close();
return null;
}
//=============================导出pdf表===========================================
public String pdfprint()throws Exception{
this.getData();
//设置响应方式
response.setContentType("application/x-download");
String name = URLEncoder.encode(table+"报表.pdf", "UTF-8");
response.setHeader("Content-Disposition", "attachment;filename="+name);
//创建一个空的pdf文档
com.lowagie.text.Document doc = new com.lowagie.text.Document(PageSize.A4.rotate());//rotate纸张横向
//将pdf往浏览器输出
PdfWriter writer = PdfWriter.getInstance(doc, response.getOutputStream());
doc.open();
//引入字体
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
//表头字体
Font font = new Font(bf,8,Font.BOLD,Color.RED);
//表格体字体
Font font1 = new Font(bf,10,Font.COURIER,Color.black);
//创建表格
Table tab = new Table(key_names.length, objList.size());
tab.setPadding(3);
//生成表头
for (int i = 0; i < tableHead.length; i++) {
Chunk ck = new Chunk(tableHead[i],font);
com.lowagie.text.Cell cell = new com.lowagie.text.Cell(ck);
tab.addCell(cell);
//设置横向居中
cell.setHorizontalAlignment(com.lowagie.text.Cell.ALIGN_CENTER);
//设置纵向居中
cell.setVerticalAlignment(com.lowagie.text.Cell.ALIGN_MIDDLE);
}
//生成表格体
for (int i = 0; i < objList.size(); i++) {
for (int j = 0; j < ziduan.length; j++) {
ziduan[j].setAccessible(true);
Chunk ck_name = new Chunk(ziduan[j].get(objList.get(i))+"",font1);
com.lowagie.text.Cell c = new com.lowagie.text.Cell(ck_name);
tab.addCell(c);
}
}
//循环设置居中
for (int i = 0; i <= objList.size(); i++) {
for (int j = 0; j < key_names.length; j++) {
com.lowagie.text.Cell cell = (com.lowagie.text.Cell)tab.getElement(i, j);
//设置横向居中
cell.setHorizontalAlignment(com.lowagie.text.Cell.ALIGN_CENTER);
//设置纵向居中
cell.setVerticalAlignment(com.lowagie.text.Cell.ALIGN_MIDDLE);
}
}
doc.add(tab);//将表格放入pdf文档
doc.close();//操作完毕,关闭文档对象
writer.close();
doc.close();
return null;
}
//=============================导出xml表===========================================
public String xmlprint() throws Exception{
this.getData();
//设置响应方式
response.setContentType("application/vnd.ms-xml;charset=utf-8");
String name = URLEncoder.encode(table+"数据报表.xml", "UTF-8");
response.setHeader("Content-Disposition", "attachment;filename="+name);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.newDocument();//空白文档
Element root = doc.createElement("table");//创建根节点
doc.appendChild(root);
for (int i = 0; i < tableHead.length; i++) {
Element entity = doc.createElement(table);
Element shux = doc.createElement(key_names[i]);
shux.setTextContent(tableHead[i]);
entity.appendChild(shux);
root.appendChild(entity);
}
for (int i = 0; i < objList.size(); i++) {
for (int j = 0; j < ziduan.length; j++) {
ziduan[j].setAccessible(true);
Element entity = doc.createElement(table);
Element shux = doc.createElement(key_names[j]);
shux.setTextContent(ziduan[j].get(objList.get(i))+"");
entity.appendChild(shux);
root.appendChild(entity);
}
}
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
DOMSource ds = new DOMSource(doc);//指定输出源
//StreamResult sr = new StreamResult(new File("f:/"+table+".xml"));
StreamResult sr = new StreamResult(response.getOutputStream());
tf.transform(ds, sr);
return null;
}
解决方案四:
巧用Java程序把Word转换成Html文件
用Java程序把Word转换成Html文件
----------------------