導航:首頁 > 編程語言 > java實現excel導出

java實現excel導出

發布時間:2022-04-15 20:55:15

java如何另存導出Excel

1./**

* 出險信息導出到excel(fc)

* @param mapping

* @param form

* @param request

* @param response

* @throws IOException

*/

public void exportActoExcel(ActionMapping mapping, ActionForm form ,

HttpServletRequest request,HttpServletResponse response) throws IOException {

ActionErrors errors = new ActionErrors();

AcExcelBusi acBusi = new AcExcelBusi();

AccidentRecordForm arForm= (AccidentRecordForm) form;

AccidentRecordBusi arBusi = new AccidentRecordBusi();

// ////查詢條件

FwUsers sessUser = (FwUsers)request.getSession().getAttribute(ConstValues.SESS_USER_MANAGE);

Map<String,Object> cisMap = arBusi.getTodoPageList(arForm,sessUser,errors);

List AcList = null;// 當頁的記錄

if (null != cisMap) {

AcList = (List) cisMap.get("list");

}


//導出excel的路徑、文件名

String uuid = UUID.create("exp");

String path = request.getSession().getServletContext().getRealPath("/") + ConstValues.EXP_PATH_EXCEL + uuid + ".xls";

acBusi.exprotAcExcel(AcList, path,request);


response.sendRedirect("stdownload.jsp?path=" + path );


}

② java導入導出excel是怎麼實現的

你可以使用poi框架。apache出的專門用於java導入導出office

③ java怎麼實現選擇導出excel的功能

import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class CreateSimpleExcelToDisk
{
/**
* @功能:手工構建一個簡單格式的Excel
*/
private static List<Student> getStudent() throws Exception
{
List list = new ArrayList();
SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");

Student user1 = new Student(1, "張三", 16, df.parse("1997-03-12"));
Student user2 = new Student(2, "李四", 17, df.parse("1996-08-12"));
Student user3 = new Student(3, "王五", 26, df.parse("1985-11-12"));
list.add(user1);
list.add(user2);
list.add(user3);

return list;
}

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);
Student stu = (Student) 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怎麼導出excel表格

通過這個例子,演示以下如何用java生成excel文件:
import org.apache.poi.hssf.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;
publicclass CreateCells
{
publicstaticvoid main(String[] args)
throws IOException
{
HSSFWorkbook wb = new HSSFWorkbook();//建立新HSSFWorkbook對象
HSSFSheet sheet = wb.createSheet("new sheet");//建立新的sheet對象
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short)0);//建立新行
// Create a cell and put a value in it.
HSSFCell cell = row.createCell((short)0);//建立新cell
cell.setCellValue(1);//設置cell的整數類型的值
// Or do it on one line.
row.createCell((short)1).setCellValue(1.2);//設置cell浮點類型的值
row.createCell((short)2).setCellValue("test");//設置cell字元類型的值
row.createCell((short)3).setCellValue(true);//設置cell布爾類型的值
HSSFCellStyle cellStyle = wb.createCellStyle();//建立新的cell樣式
cellStyle.setDataFormat(HSSFDataFormat.getFormat("m/d/yy h:mm"));//設置cell樣式為定製的日期格式
HSSFCell dCell =row.createCell((short)4);
dCell.setCellValue(new Date());//設置cell為日期類型的值
dCell.setCellStyle(cellStyle); //設置該cell日期的顯示格式
HSSFCell csCell =row.createCell((short)5);
csCell.setEncoding(HSSFCell.ENCODING_UTF_16);//設置cell編碼解決中文高位位元組截斷
csCell.setCellValue("中文測試_Chinese Words Test");//設置中西文結合字元串
row.createCell((short)6).setCellType(HSSFCell.CELL_TYPE_ERROR);//建立錯誤cell
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
}

⑤ java excel導出到用戶本地

一般來說做下載功能的確是先導到伺服器的一個臨時目錄上的,然後再用一段代碼把這個excel讀出來,並且輸出到response流裡面去,給你一段可以用的代碼

//------------------------------
//step1.保存一個臨時excel到temp目錄下
//------------------------------
//這部分自己實現,我相信你已經實現了
//假設你已經實現了保存一個excel到一個臨時文件夾裡面去
//並且已經生成了一個File指向這個臨時的excel,名叫exportFile

//-------------------------------
//step2.彈出下載對話框
//-------------------------------
if(exportFile==null){
logger.error("生成excel錯誤!exportFile為空");
return;
}

//先建立一個文件讀取流去讀取這個臨時excel文件
FileInputStreamfs=null;
try{
fs=newFileInputStream(exportFile);
}catch(FileNotFoundExceptione){
logger.error("生成excel錯誤!"+exportFile+"不存在!",e);
return;
}
//設置響應頭和保存文件名
HttpServletResponseresponse=ServletActionContext.getResponse();
//這個一定要設定,告訴瀏覽器這次請求是一個下載的數據流
response.setContentType("APPLICATION/OCTET-STREAM");
try{
//這邊的"采購部門本月采購報表.xls"替換成你自己要顯示給用戶的文件名
excelName=URLEncoder.encode("采購部門本月采購報表.xls","UTF-8");
}catch(){
logger.error("轉換excel名稱編碼錯誤!",e1);
}
response.setHeader("Content-Disposition","attachment;filename=""+excelName+""");
//寫出流信息
intb=0;
try{
//這里的response就是你servlet的那個傳參進來的response
PrintWriterout=response.getWriter();
while((b=fs.read())!=-1){
out.write(b);
}
fs.close();
out.close();
logger.debug(sheetName+"文件下載完畢.");
}catch(Exceptione){
logger.error(sheetName+"下載文件失敗!.",e);
}


把這段代碼放到你的servlet的最後一部分

⑥ 如何用JAVA導出Excel(使用POI)

// 創建臨時文件(excel為Workbook對象)
response.reset();
response.setContentType("application/download");
response.setHeader("Content-Disposition",
"attachment;filename=" + excel.getFileName());
excel.getExcel().write(response.getOutputStream());
response.flushBuffer();

⑦ java導出excel

java導出Excel java 代碼 /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package com.axon.fable.sams.view.action; import java.io.IOException; import java.io.OutputStream; import java.util.List; import javax.serv ... java導出Excel例舉方式 方法一:導出Excel數據的插件jexcelapi 程序實例如下: public void exportClassroom(OutputStream os) throws PaikeException { try { WritableWorkbook wbook = Workbook.createWorkbook(os); //建立excel文件 WritableSheet wsheet = wbook.createSheet("教室信息表", 0); //工作表名稱 //設置Excel字體 WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16, WritableFont.BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); WritableCellFormat titleFormat = new WritableCellFormat(wfont); String[] title = { "教室名", "容 量", "類 型", "其他說明" }; //設置Excel表頭 for (int i = 0; i < title.length; i++) { Label excelTitle = new Label(i, 0, title[i], titleFormat); wsheet.addCell(excelTitle); } int c = 1; //用於循環時Excel的行號 ClassroomService cs = new ClassroomService(); List list = cs.findAllClassroom(); //這個是從資料庫中取得要導出的數據 Iterator it = list.iterator(); while (it.hasNext()) { ClassroomDTO crdto = (ClassroomDTO) it.next(); Label content1 = new Label(0, c, crdto.getRoomname()); Label content2 = new Label(1, c, crdto.getCapicity().toString()); Label content3 = new Label(2, c, crdto.getRoomTypeId() .toString()); Label content4 = new Label(3, c, crdto.getRemark()); wsheet.addCell(content1); wsheet.addCell(content2); wsheet.addCell(content3); wsheet.addCell(content4); c++; } wbook.write(); //寫入文件 wbook.close(); os.close(); } catch (Exception e) { throw new PaikeException("導出文件出錯"); } } 方法二:直接用Java代碼實現導出Excel報表 /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */

⑧ java中,如何實現將導出的excel導出的數據條數及無法導出數據提示在頁面

將數據表中的數據導出並保存為xls簡單,用SSIS或者查詢出來之後另存為都可以。但是,這個表中的分類有數百個,如果一個個用SSIS或者查詢另存為的話,工作量巨大。前思後想,想到了用while循環查詢,並用bcp導出的方法。下面是相關代碼:
--聲明需要的變數

set @sql=@sql+' union all select 列名1,列名2,列名3 from t_TestTable where TypeID='+cast(@TypeID as varchar(8))+')a queryout F:\datafile\TypeData'+cast(@TypeID as varchar(8))+'.xls -c -q -SServerName -Usa -PSAPASSWORD -dDBName'--查詢滿足條件的記錄並保存到xls文件中

⑨ java端導出Excel表格。

可以使用poi來實現導出execl表格或者通過io流實現導出execl表格,但是poi相對來說更方便
實例如下:
try{
HSSFWorkbook workbook = new HSSFWorkbook(); // 創建工作簿對象
HSSFSheet sheet = workbook.createSheet(title); // 創建工作表

// 產生表格標題行
HSSFRow rowm = sheet.createRow(0);
HSSFCell cellTiltle = rowm.createCell(0);

//sheet樣式定義【getColumnTopStyle()/getStyle()均為自定義方法 - 在下面 - 可擴展】
HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);//獲取列頭樣式對象
HSSFCellStyle style = this.getStyle(workbook); //單元格樣式對象

sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, (rowName.length-1)));
cellTiltle.setCellStyle(columnTopStyle);
cellTiltle.setCellValue(title);

// 定義所需列數
int columnNum = rowName.length;
HSSFRow rowRowName = sheet.createRow(2); // 在索引2的位置創建行(最頂端的行開始的第二行)

// 將列頭設置到sheet的單元格中
for(int n=0;n<columnNum;n++){
HSSFCell cellRowName = rowRowName.createCell(n); //創建列頭對應個數的單元格
cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); //設置列頭單元格的數據類型
HSSFRichTextString text = new HSSFRichTextString(rowName[n]);
cellRowName.setCellValue(text); //設置列頭單元格的值
cellRowName.setCellStyle(columnTopStyle); //設置列頭單元格樣式
}

//將查詢出的數據設置到sheet對應的單元格中
for(int i=0;i<dataList.size();i++){

Object[] obj = dataList.get(i);//遍歷每個對象
HSSFRow row = sheet.createRow(i+3);//創建所需的行數

for(int j=0; j<obj.length; j++){
HSSFCell cell = null; //設置單元格的數據類型
if(j == 0){
cell = row.createCell(j,HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(i+1);
}else{
cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING);
if(!"".equals(obj[j]) && obj[j] != null){
cell.setCellValue(obj[j].toString()); //設置單元格的值
}
}
cell.setCellStyle(style); //設置單元格樣式
}
}
//讓列寬隨著導出的列長自動適應
for (int colNum = 0; colNum < columnNum; colNum++) {
int columnWidth = sheet.getColumnWidth(colNum) / 256;
for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
HSSFRow currentRow;
//當前行未被使用過
if (sheet.getRow(rowNum) == null) {
currentRow = sheet.createRow(rowNum);
} else {
currentRow = sheet.getRow(rowNum);
}
if (currentRow.getCell(colNum) != null) {
HSSFCell currentCell = currentRow.getCell(colNum);
if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
int length = currentCell.getStringCellValue().getBytes().length;
if (columnWidth < length) {
columnWidth = length;
}
}
}
}
if(colNum == 0){
sheet.setColumnWidth(colNum, (columnWidth-2) * 256);
}else{
sheet.setColumnWidth(colNum, (columnWidth+4) * 256);
}
}

if(workbook !=null){
try
{
String fileName = "Excel-" + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls";
String headStr = "attachment; filename=\"" + fileName + "\"";
response = getResponse();
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", headStr);
OutputStream out = response.getOutputStream();
workbook.write(out);
}
catch (IOException e)
{
e.printStackTrace();
}
}

}catch(Exception e){
e.printStackTrace();
}

}

閱讀全文

與java實現excel導出相關的資料

熱點內容
php獨立運行 瀏覽:530
手機sh執行命令 瀏覽:727
雲伺服器的角色 瀏覽:733
單片機頻率比例 瀏覽:840
我的世界伺服器如何關閉正版驗證 瀏覽:504
如何查roid伺服器上的 瀏覽:130
安卓手機主板如何撬晶元不掉電 瀏覽:249
php各個框架的優缺點 瀏覽:101
php1100生成數組 瀏覽:359
以後做平面設計好還是程序員好 瀏覽:552
雲伺服器應用管理 瀏覽:438
飢荒雲伺服器搭建過程 瀏覽:186
可編程式控制制器優點 瀏覽:99
壓縮垃圾車說明書 瀏覽:28
五輪書pdf 瀏覽:802
單片機定時流水中斷系統流水燈 瀏覽:701
u8如何連接伺服器配置 瀏覽:68
動力在於緩解壓力 瀏覽:867
報考科一用什麼app 瀏覽:346
knn人臉識別演算法 瀏覽:431