자바 엑셀 내 보 내기 실현
Excel
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.FileOutputStream;
import java.io.FileInputStream;
public class CreateXL
{
public static String xlsFile="test.xls"; // Excel
public static void main(String args[])
{
try
{
HSSFWorkbook workbook = new HSSFWorkbook(); //
HSSFSheet sheet = workbook.createSheet(); //
// firstSheet
// , UTF_16
workbook.setSheetName(0,"firstSheet",HSSFWorkbook.ENCODING_UTF_16);
//
HSSFRow row = sheet.createRow((short)0);
//
HSSFCell cell = row.createCell((short) 0);
//
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
// , UTF_16。
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
//
cell.setCellValue(" ");
FileOutputStream fOut = new FileOutputStream(xlsFile);
workbook.write(fOut);
fOut.flush();
fOut.close();
System.out.println(" ...");
// Excel
FileInputStream fIn=new FileInputStream(xlsFile);
HSSFWorkbook readWorkBook= new HSSFWorkbook(fIn);
HSSFSheet readSheet= readWorkBook.getSheet("firstSheet");
HSSFRow readRow =readSheet.getRow(0);
HSSFCell readCell = readRow.getCell((short)0);
System.out.println(" :" + readCell.getStringCellValue());
}
catch(Exception e)
{
System.out.println(e);
}
}
}
POI, JDBC , Excel 。 :
/* rs Excel 。
* : rs,Excel xlsName, sheetName。
*/
public static void resultSetToExcel(ResultSet rs,String xlsName,String sheetName) throws Exception
{
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
workbook.setSheetName(0,sheetName,HSSFWorkbook.ENCODING_UTF_16);
HSSFRow row= sheet.createRow((short)0);;
HSSFCell cell;
ResultSetMetaData md=rs.getMetaData();
int nColumn=md.getColumnCount();
//
for(int i=1;i<=nColumn;i++)
{
cell = row.createCell((short)(i-1));
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(md.getColumnLabel(i));
}
int iRow=1;
// , Excel
while(rs.next())
{row= sheet.createRow((short)iRow);;
for(int j=1;j<=nColumn;j++)
{
cell = row.createCell((short)(j-1));
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(rs.getObject(j).toString());
}
iRow++;
}
FileOutputStream fOut = new FileOutputStream(xlsName);
workbook.write(fOut);
fOut.flush();
fOut.close();
JOptionPane.showMessageDialog(null," !");
}
POI , 、 .
Excel
package zjx;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import dao.ProductDao;
import dao.impl.ProductDaoImpl;
import entity.Product;
import jxl.*;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.CellFormat;
import jxl.write.Boolean;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class DemoXls {
/**
* Excel
*/
public static void main(String[] args) {
// excel
String[] title = {" "," "," "," "};
try {
//
long start = System.currentTimeMillis();
// excel
String filePath = "e:\\testJXL2.xls";
// Excel
WritableWorkbook wwb;
// jxl , e testJXL.xls
OutputStream os = new FileOutputStream(filePath);
wwb=Workbook.createWorkbook(os);
// Sheet
WritableSheet sheet = wwb.createSheet(" ", 0);
Label label;
for(int i=0;i<title.length;i++){
// Label(x,y,z) x+1 , y+1 , z
// Label
label = new Label(i,0,title[i]);
//
sheet.addCell(label);
}
//
/*
* , jxl.write.Number
* ,
* */
ProductDao dao=new ProductDaoImpl();
List<Product> list=dao.queryAll();
for(int i=0;i<list.size();i++){
sheet.setRowView(i,400);
//
jxl.write.Number number = new jxl.write.Number(0,i+1,list.get(i).getProductId());
sheet.addCell(number);
//
label = new Label(1,i+1,list.get(i).getProductName());
sheet.addCell(label);
jxl.write.Number type = new jxl.write.Number(2,i+1,list.get(i).getProductTypeId());
sheet.addCell(type);
jxl.write.Number measure = new jxl.write.Number(3,i+1,list.get(i).getMeasureTypeId());
sheet.addCell(measure);
Label label2 = new Label(1,i+1,list.get(i).getSource());
sheet.addCell(label2);
}
//
wwb.write();
//
wwb.close();
long end = System.currentTimeMillis();
System.out.println("---- :"+(end-start)/1000);
} catch (Exception e) {
System.out.println("--- ---");
e.printStackTrace();
}
}
}
엑셀 표 다운로드
public String exportReport(){
accounts = (SysAccounts) getRequest().getSession().getAttribute(
"SYSaccount");
if (!"".equals(accounts) && null!= accounts ) {
String fp = "/report/posquery/";
Long file = (new Date()).getTime();
System.out.print(" ");
Map map=null;
listmap=new ArrayList<Map>();
List datelist=Dateutil.datelist(ymouse);//
String hql="from "+Orgbank.class.getName()+" where bankno='"+bankno+"'";
List lists=bankbfjreportService.find(hql);
if(lists!=null&&lists.size()>0){
bankacct=(Orgbank) lists.get(0);
}
System.out.print(sql);
list=bankbfjreportService.getListFromSQL(sql);
Double bbputamt=0.0;
Double bbputfeeamt=0.0;
Double qbputamt=0.0;
Double qbputfeeamt=0.0;
Double bysputamt=0.0;
Double bysputfeeamt=0.0;
Integer trandate1=0;
for(int i=1;i<=datelist.size();i++){
map=new HashMap();
map.put("date", i);
if(list!=null&&list.size()>0){
Object[] obj=null;
int kk=0;
for(int j=0;j<list.size();j++){
if(null!=obj[0]){
String date=obj[0].toString();
date=date.substring(7, 8);
trandate1=Integer.valueOf(date);
}
if(null!=obj[1]){
bbputamt=Double.valueOf(obj[1].toString());
}
if(null!=obj[2]){
bbputfeeamt=Double.valueOf(obj[2].toString());
}
if(null!=obj[3]){
qbputamt=Double.valueOf(obj[3].toString());
}
if(null!=obj[4]){
qbputfeeamt=Double.valueOf(obj[4].toString());
}
if(null!=obj[5]){
bysputamt=Double.valueOf(obj[5].toString());
}
if(null!=obj[6]){
bysputfeeamt=Double.valueOf(obj[6].toString());
}
if(i==trandate1){
kk+=1;
map.put("A01", bbputamt);
map.put("A02", bbputfeeamt);
map.put("A03", qbputamt);
map.put("A04", qbputfeeamt);
map.put("A05", bysputamt);
map.put("A06", bysputfeeamt);
map.put("A07", 0);
map.put("A08", 0);
map.put("A09", 0);
map.put("A10", 0);
map.put("A11", 0);
map.put("A12", 0);
map.put("A13", 0);
map.put("A14", 0);
}
}
if(kk!=1){
map.put("A01", 0);
map.put("A02", 0);
map.put("A03", 0);
map.put("A04", 0);
map.put("A05", 0);
map.put("A06", 0);
map.put("A07", 0);
map.put("A08", 0);
map.put("A09", 0);
map.put("A10", 0);
map.put("A11", 0);
map.put("A12", 0);
map.put("A13", 0);
map.put("A14", 0);
listmap.add(map);
}
}else{
map.put("A01", 0);
map.put("A02", 0);
map.put("A03", 0);
map.put("A04", 0);
map.put("A05", 0);
map.put("A06", 0);
map.put("A07", 0);
map.put("A08", 0);
map.put("A09", 0);
map.put("A10", 0);
map.put("A11", 0);
map.put("A12", 0);
map.put("A13", 0);
map.put("A14", 0);
System.out.println(map.get("date")+"---------------");
listmap.add(map);
}
}
if (listmap.size() > 65000) {
setMessage("<script>alert(' 65000 , !');location.href='pro.do';</script>");
return ERROR;
}
ZfrjdetailAction ce = new ZfrjdetailAction();
ce.createExcel(bankbfjreportService,ymouse,bankno,getRequest(), listmap, fp, file.toString()
+ ".xls");
String path = fp + file.toString() + ".xls";
try {
HttpServletResponse response = this.getResponse();
response.setCharacterEncoding("UTF-8");
PrintWriter o = response.getWriter();
StringBuffer ou = new StringBuffer();
// ou.append("<script>alert('asdf');location.href='/giftweb/Downlaod'</script>");
ou.append("<script>top.location.href='/giftweb/Downlaod?file="
+ path + "';history.go(-1);</script>");
// ou.append("<script>location.href='/Downlaod?file=/report/posquery/1392729094571.xls';hisory.go(-1);</script>");
System.err.println(ou.toString());
o.write(ou.toString());
o.flush();
o.close();
} catch (Exception e) {
e.printStackTrace();
}
} else {
setMessage("<script>alert(\" , !\");top.location.href='/giftweb/admin/logining.html';</script>");
return ERROR;
}
return ERROR;
}
스타일 링크 설정:
http://zhouhaitao.iteye.com/blog/1842769
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.