String str = request.getParameter("str");
str = URLDecoder.decode(str, "utf-8");
String[] arr = str.split(",");
try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("第一个xls");
HSSFCellStyle cellStyle = wb.createCellStyle(); //字体用的风格
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //左右居中
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //上下居中
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); //左边框
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); //右边框
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); //上边框
cellStyle.setWrapText(true);
HSSFFont cellFont5 = wb.createFont();
cellFont5.setFontHeightInPoints((short) 10); //字号
cellFont5.setFontName("宋体"); //设置为宋体
cellStyle.setFont(cellFont5);
HSSFRow rowi = sheet.createRow(0); //第一行
rowi.setHeight((short)800);
HSSFCell celli = rowi.createCell((short) 0);
celli.setEncoding(HSSFCell.ENCODING_UTF_16);
HSSFCell cellmy = null;
for (int i = 0; i < 4; i++) {
cellmy = rowi.createCell((short) i);
cellmy.setCellStyle(cellStyle);
}
celli.setCellValue("生成Excel文件");
celli.setCellStyle(cellStyle);
sheet.addMergedRegion(new Region(0, (short) 0, 0, (short)4));
HSSFRow row = sheet.createRow(1); // 第一行标题
String[] titles = { "姓名", "年龄", "QQ", "Email" };
HSSFCell cell = null;
for (short i = 0; i < titles.length; i++) {
cell = row.createCell(i);
cell.setCellValue(titles[i]);
}
row = sheet.createRow(2); // 第二行
for (short i = 0; i < arr.length; i++) {
cell = row.createCell(i);
cell.setCellValue(arr[i]);
}
SimpleDateFormat timeformat = new SimpleDateFormat("yyyyMMddHHmmss");
java.util.Date date = Calendar.getInstance().getTime();
String createdate = timeformat.format(date).toString();
int location = (int) (Math.random() * 100); // 100中取出一个
String fileDirectory = request.getRealPath("/") + "/xls";
FileOutputStream fileOut = new FileOutputStream(fileDirectory
+ File.separator + createdate + "" + location + ".xls");
wb.write(fileOut);
fileOut.flush();
fileOut.close();
String fileName = fileDirectory + File.separator + createdate + ""
+ location + ".xls";
// System.out.println("文件名" + fileName);
// 新建一个SmartUpload对象
SmartUpload su = new SmartUpload();
su.initialize(this.getServletConfig(), request, response);
su.downloadFile(fileName, "application/vnd.ms-excel");// 用浏览器打开
} catch (Exception e) {
e.printStackTrace();
}
<%@ page language="java" pageEncoding="gbk"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<meta http-equiv="content-type" content="text/html; charset=gbk">
<script type="text/javascript">
function generateExcel(e){
// 去掉字符两端的空白字符
String.prototype.Trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
var str="";
var ele=null;
var tr=null;
if(document.all){
ele=event.srcElement;
tr=ele.parentElement.parentElement;
}else{
ele=e.target;
tr=ele.parentNode.parentNode;
}
var len=tr.childNodes.length;
//alert(len);
for(var i=0;i<len-1;i++){
var child=tr.childNodes[i];
if(document.all){
str+=child.innerText.Trim()+",";
}else{
if(child.tagName){
if(child.textContent!=null){
str+=child.textContent.Trim()+",";
}
//alert(child.textContent);
}
}
}
//alert(str);
str=str.substr(0,str.length-1);
//alert(str);
str=encodeURI(str);
str=encodeURI(str);
document.location="makeExcel?str="+str;
}
</script>
</head>
<body>
<table border="1" cellspacing="0" cellpadding="6">
<tr>
<td>
姓名
</td>
<td>
年龄
</td>
<td>
QQ
</td>
<td>
Email
</td>
<td>
操作
</td>
</tr>
<tr>
<td>
张三
</td>
<td>
18
</td>
<td>
359951165
</td>
<td>
weiweichen_666@126.com
</td>
<td>
<input type="button" value="生成excel文件"
onClick="generateExcel(event)"></input>
</td>
</tr>
<tr>
<td>
李四
</td>
<td>
21
</td>
<td>
2221311
</td>
<td>
weiweichen_8888@126.com
</td>
<td>
<input type="button" value="生成excel文件"
onClick="generateExcel(event)"></input>
</td>
</tr>
<tr>
<td>
李四光
</td>
<td>
100
</td>
<td>
1234567
</td>
<td>
siguang@163.com
</td>
<td>
<input type="button" value="生成excel文件"
onClick="generateExcel(event)"></input>
</td>
</tr>
<tr>
<td>
小飞
</td>
<td>
32
</td>
<td>
2322223
</td>
<td>
ssss@222.com
</td>
<td>
<input type="button" value="生成excel文件"
onClick="generateExcel(event)"></input>
</td>
</tr>
<tr>
<td>
叶无道
</td>
<td>
20
</td>
<td>
7654321
</td>
<td>
wwww@222.com
</td>
<td>
<input type="button" value="生成excel文件"
onClick="generateExcel(event)"></input>
</td>
</tr>
</table>
</body>
</html>
分享到:
相关推荐
poi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成word
poi转换xls到xlsx
Java使用poi-tl生成word文档
poi操作ppt生成图表完整工程
使用poi将xls格式的Excel转为xlsx,文件中的所有样式都可以完美转换,例如合并栏、背景和单元格样式等。Java源码
Flex DataGrid导出成excel的方法,介绍了导出的两种方式poi和as3xls
poi-3.0.2 生成excel表工具,java生成excel表格时使用
java实现poi模板生成PPT文件代码:两个迭代版本。java实现poi模板生成PPT文件代码:两个迭代版本。
本人文章《使用poi根据模版生成word文档并转换成PDF文件》所需要用到的文件
使用poi导出excel支持xls、xlsx格式大数据导出java工具类,支持同一个单元格不同文本格式。找了好久,加上自己修改后,调试通过,发布出来分享,(调整student类位置)包含所需jar包,工具类,使用示例
Java POI 生成Word文档,支持图片插入,关键是修改XML部分,本人已测试通过。
Java POI根据模板生成Excel文件并写入磁盘,资源文件仅为实现的简单测试Demo,并没有进行代码优化,可以直接导入运行,资源文件仅供参考。
包含poi 3.9的jar 和其它相关的jar ,xmlbeans.jar 和org.slf4j.jar 还包含一个测试用例。
对目录样式,布局,标题,位置的修改,目录对应的页码只能手动设置,无法自动获取,详情:https://blog.csdn.net/qq1223739055/article/details/101440864
POI操作Excel生成水印,该水印其实就是一个图片,该代码是将文字生成图片,再将图片变成水印的。
使用java Apache poi 根据word模板生成word报表 仅支持docx格式的word文件,大概是word2010及以后版本,doc格式不支持。 使用说明:https://blog.csdn.net/u012775558/article/details/79678701
技术使用poi,实际功能为html文件转换成word, 生成pdf格式合同,html生成图片,生成html格式合同,下载解压缩即可运行