java怎么用poi导出到Excle。年度考核汇总跨列成这个样式
给你一段代码参考一下。
<%
String filename ="武汉光谷建设投资有限公司"+request.getAttribute("year").toString()+request.getAttribute("month").toString()+"进度报表.xls";
response.setContentType("application/msexcel;charset=UTF-8");
response.setHeader("Content-disposition","attachment;filename="+ new String(filename.getBytes("GBK"),("iso-8859-1")));
%>
在jsp加上这段代码然后在用table布局即可
JAVA,POI导出EXCEL表,表中所有数据都是从后台直接获取,求指导,越详细越好
举个例子吧
public class CreateSimpleExcelToDisk
{
/**
* @功能:手工构建一个简单格式的Excel
*/
public static List getStudent(String[] str) throws Exception
{
List listts = new ArrayList();
SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
Connection conn = DBUtil.getCon();
String sqlback="";
for(int i=0;i<str.length-1;i++){
sqlback=sqlback+"\"id\"='"+str[i]+"'or";
}
sqlback=sqlback+"\"id\"='"+str[str.length-1]+"'";
String sql1 = "select * from 表名 where"+sqlback;
System.out.println(sql1 );
ResultSet rs = DBUtil.getResult(conn, sql1);
String outStr = "";
int a=1;
try {
while(rs.next()){
String objid = rs.getString(1);
String tsname= rs.getString("tsname");
String tstel= rs.getString("tstel");
String tscpname= rs.getString("tscpname");
String tsads=rs.getString("tsads");
String tsqus=rs.getString("tsqus");
String tsno=rs.getString("tsno");
String tsdate=rs.getString("tsdate");
Tsxx user = new Tsxx(a,tsname,tstel,tscpname,tsads,tsqus,tsno,df.parse(tsdate));
listts.add(user);
a++;
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if (rs != null) {
rs.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
}}
return listts;
}
public static void main(String[] args) throws Exception
{
// // 第一步,创建一个webbook,对应一个Excel文件
// HSSFWorkbook wb = new HSSFWorkbook();
// // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
// HSSFSheet sheet = wb.createSheet("学生表一");
// // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
// HSSFRow row = sheet.createRow((int) 0);
// // 第四步,创建单元格,并设置值表头 设置表头居中
// HSSFCellStyle style = wb.createCellStyle();
// style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
//
// HSSFCell cell = row.createCell((short) 0);
// cell.setCellValue("学号");
// cell.setCellStyle(style);
// cell = row.createCell((short) 1);
// cell.setCellValue("姓名");
// cell.setCellStyle(style);
// cell = row.createCell((short) 2);
// cell.setCellValue("年龄");
// cell.setCellStyle(style);
// cell = row.createCell((short) 3);
// cell.setCellValue("生日");
// cell.setCellStyle(style);
//
// // 第五步,写入实体数据 实际应用中这些数据从数据库得到,
// List list = CreateSimpleExcelToDisk.getStudent();
//
// for (int i = 0; i < list.size(); i++)
// {
// row = sheet.createRow((int) i + 1);
// Tsxx stu = (Tsxx) list.get(i);
// // 第四步,创建单元格,并设置值
// row.createCell((short) 0).setCellValue((double) stu.getId());
// row.createCell((short) 1).setCellValue(stu.getName());
// row.createCell((short) 2).setCellValue((double) stu.getAge());
// cell = row.createCell((short) 3);
// cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu.getBirth()));
// }
// // 第六步,将文件存到指定位置
// try
// {
// FileOutputStream fout = new FileOutputStream("E:/students.xls");
// wb.write(fout);
// fout.close();
// }
// catch (Exception e)
// {
// e.printStackTrace();
// }
}
java用poi往excel里写数据遇到换行问题
String[] arr = new String[] { "a", "b", "c", "d", "a", "b", "c", "d", "a", "b", "c", "d" };
List list = Arrays.asList(arr);
if (list != null) {
StringBuffer buff = new StringBuffer();
int colIndex = 0;// 用来标识列下标
for (int i = 0; i < list.size(); i++) {
if (i != 0 && i % 3 == 0) {// 不是第一个下标并且能被3整除,也就是4个一个轮回
buff.append(list.get(i));
rows.createCell(colIndex++).setCellValue(new String(buff));
buff = new StringBuffer();// 清空buff内容
} else {
buff.append(list.get(i));
}
}
}
poi导出excel表格,在导出的时候能不能设置内容换行
在开发中经常需要用到对Excel文件的操作,POI生成excel实现自动调整行高的代码如下:
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFComment;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
public class PoiCreateExcelTest {
public static void main(String[] args) {
/**
* @see For more
*/
// 创建新的Excel 工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
// 在Excel工作簿中建一工作表,其名为缺省值, 也可以指定Sheet名称
HSSFSheet sheet = workbook.createSheet();
//HSSFSheet sheet = workbook.createSheet("SheetName");
// 用于格式化单元格的数据
HSSFDataFormat format = workbook.createDataFormat();
// 创建新行(row),并将单元格(cell)放入其中. 行号从0开始计算.
HSSFRow row = sheet.createRow((short) 1);
// 设置字体
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) 20); //字体高度
font.setColor(HSSFFont.COLOR_RED); //字体颜色
font.setFontName("黑体"); //字体
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //宽度
font.setItalic(true); //是否使用斜体
// font.setStrikeout(true); //是否使用划线
// 设置单元格类型
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局:居中
cellStyle.setWrapText(true);
// 添加单元格注释
// 创建HSSFPatriarch对象,HSSFPatriarch是所有注释的容器.
HSSFPatriarch patr = sheet.createDrawingPatriarch();
// 定义注释的大小和位置,详见文档
HSSFComment comment = patr.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short)4, 2, (short) 6, 5));
// 设置注释内容
comment.setString(new HSSFRichTextString("可以在POI中添加注释!"));
// 设置注释作者. 当鼠标移动到单元格上是可以在状态栏中看到该内容.
comment.setAuthor("Xuys.");
// 创建单元格
HSSFCell cell = row.createCell((short) 1);
HSSFRichTextString hssfString = new HSSFRichTextString("Hello World!");
cell.setCellValue(hssfString);//设置单元格内容
cell.setCellStyle(cellStyle);//设置单元格样式
cell.setCellType(HSSFCell.CELL_TYPE_STRING);//指定单元格格式:数值、公式或字符串
cell.setCellComment(comment);//添加注释
//格式化数据
row = sheet.createRow((short) 2);
cell = row.createCell((short) 2);
cell.setCellValue(11111.25);
cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(format.getFormat("0.0"));
cell.setCellStyle(cellStyle);
row = sheet.createRow((short) 3);
cell = row.createCell((short) 3);
cell.setCellValue(9736279.073);
cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(format.getFormat("#,##0.0000"));
cell.setCellStyle(cellStyle);
sheet.autoSizeColumn((short)0); //调整第一列宽度
sheet.autoSizeColumn((short)1); //调整第二列宽度
sheet.autoSizeColumn((short)2); //调整第三列宽度
sheet.autoSizeColumn((short)3); //调整第四列宽度
try {
FileOutputStream fileOut = new FileOutputStream("C:/3.xls");
workbook.write(fileOut);
fileOut.close();
} catch (Exception e) {
System.out.println(e.toString());
}
}
}