㈠ java :怎麼實現分批次導出excel的條數限制,每次導出的excel壓縮成zip,並且導出時給用戶添加友好提示
你這問題太大了
HSSFWorkbook 是導出的工具,可是實現導出excel控制條數
//開始列印
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet=wb.createSheet("sheet1");
HSSFRow row = sheet.createRow(0);
//設置第一行標題
HSSFCell cell;
JobColumn jobColumn = null;
int colsAddSize = NewmasterstudentAction.COLS4EXPORT_CODE.length;
for(int i=0 ; i<colsAddSize ; i++){
cell = row.createCell((short)i);
cell.setCellValue(NewmasterstudentAction.COLS4EXPORT_CODE[i]+"("+NewmasterstudentAction.COLS4EXPORT_NAME[i]+")");
}
for(int i=0;i<jobList.size();i++){
UserJobColumn userJob = jobList.get(i);
jobColumn = userJob.getJobColumn();
cell = row.createCell((short)(i+colsAddSize));
cell.setCellValue(jobColumn.getColumnCode()+"("+jobColumn.getColumnName()+")"); //欄位代碼
}
//列印記錄
Set<Entry<String,List<JobRecord>>> entrySet = des.entrySet();
Iterator<Entry<String, List<JobRecord>>> it = entrySet.iterator();
int index = 1;
while(it.hasNext()){
Entry<String, List<JobRecord>> en = it.next();
row = sheet.createRow(index++);
String xh = en.getKey();
//列印學生信息
cell = row.createCell((short)0);
cell.setCellValue(xh);
NewMasterStudent stu = studentMap.get(xh);
if(stu != null){
cell = row.createCell((short)1);
cell.setCellValue(stu.getYbd() ? "是" : "否");
cell = row.createCell((short)2);
cell.setCellValue(stu.getRemark() == null ? "" : stu.getRemark());
}
//列印記錄
List<JobRecord> records = en.getValue();
for(int i=0 ; i <jobList.size() ; i++){
cell = row.createCell((short)(i+colsAddSize));
UserJobColumn g = jobList.get(i);
JobRecord record = null;
if(i < records.size()){
record = records.get(i);
if(!g.getJobColumn().getId().equals(record.getJobColumn().getId())){
record = new JobRecord();
record.setJobColumn(g.getJobColumn());
records.add(i, record);
}
}else{
record = new JobRecord();
record.setJobColumn(g.getJobColumn());
records.add(i, record);
}
if(Constant.NS_JOBCOLUMN_TYPE_INPUT.equals(record.getJobColumn().getColumnType())){
cell.setCellValue(record.getEditLr());
}else if(Constant.NS_JOBCOLUMN_TYPE_SELECT.equals(record.getJobColumn().getColumnType())){
cell.setCellValue(record.isEditState() ? "是":"否");
}else if(Constant.NS_JOBCOLUMN_TYPE_OPTION.equals(record.getJobColumn().getColumnType())){
SyGeneralCode select = record.getSelectedValue();
if(select != null){
cell.setCellValue(select.getCode()+" | "+select.getCnName());
}
}
}
}
ZipEntry 可以實現導出zip文件
if (files[i].exists() && !files[i].isDirectory()) {
String zjhmImg = files[i].getName();
if (zjhmMap.get(zjhmImg) != null) {
num++;
FileInputStream fi = new FileInputStream(files[i]);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(files[i].getName());
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
out.setEncoding("GBK");
origin.close();
㈡ 編寫java程序。使用poi根據資料庫表中某一列分別生成excel文件,最後將所有的excel以zip格式保存到D盤中
啊啊啊啊啊啊啊啊啊啊啊啊sssssssss【【;;;;;;
㈢ java中怎麼將excel轉換成zip格式
本人已經測試通過。
public static void main(String[] args) throws Exception{
InputStream inputStream = new FileInputStream("d:/customer.xls");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = 0;
byte[] buffer = new byte[1024];
while((len = inputStream.read(buffer))!= -1){
baos.write(buffer, 0, len);
}
byte[] bus = baos.toByteArray();
FileOutputStream fos= new FileOutputStream("d:/test.zip");
ZipOutputStream zos = new ZipOutputStream(fos);
zos.putNextEntry(new ZipEntry("customer.xls"));
zos.write(bus);
zos.closeEntry();
inputStream.close();
baos.close();
zos.close();
fos.close();
}
㈣ 怎樣將excel文件壓縮為zip格式
安裝一個壓縮的軟體,比如rar。
滑鼠右鍵點擊添加到壓縮文件(A),在彈出的對話框中在壓縮文件格式里把默認的rar改成zip,點確定就ok了!
㈤ 如何用java 將文件加密壓縮為zip文件.
用java加密壓縮文件:
package com.ninemax.demo.zip.decrypt;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.zip.DataFormatException;
import org.apache.commons.io.FileUtils;
import de.idyl.winzipaes.AesZipFileDecrypter;
import de.idyl.winzipaes.AesZipFileEncrypter;
import de.idyl.winzipaes.impl.AESDecrypter;
import de.idyl.winzipaes.impl.AESDecrypterBC;
import de.idyl.winzipaes.impl.AESEncrypter;
import de.idyl.winzipaes.impl.AESEncrypterBC;
import de.idyl.winzipaes.impl.ExtZipEntry;
/**
* 壓縮指定文件或目錄為ZIP格式壓縮文件
* 支持中文(修改源碼後)
* 支持密碼(僅支持256bit的AES加密解密)
* 依賴bcprov項目(bcprov-jdk16-140.jar)
*
* @author zyh
*/
public class DecryptionZipUtil {
/**
* 使用指定密碼將給定文件或文件夾壓縮成指定的輸出ZIP文件
* @param srcFile 需要壓縮的文件或文件夾
* @param destPath 輸出路徑
* @param passwd 壓縮文件使用的密碼
*/
public static void zip(String srcFile,String destPath,String passwd) {
AESEncrypter encrypter = new AESEncrypterBC();
AesZipFileEncrypter zipFileEncrypter = null;
try {
zipFileEncrypter = new AesZipFileEncrypter(destPath, encrypter);
/**
* 此方法是修改源碼後添加,用以支持中文文件名
*/
zipFileEncrypter.setEncoding("utf8");
File sFile = new File(srcFile);
/**
* AesZipFileEncrypter提供了重載的添加Entry的方法,其中:
* add(File f, String passwd)
* 方法是將文件直接添加進壓縮文件
*
* add(File f, String pathForEntry, String passwd)
* 方法是按指定路徑將文件添加進壓縮文件
* pathForEntry - to be used for addition of the file (path within zip file)
*/
doZip(sFile, zipFileEncrypter, "", passwd);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
zipFileEncrypter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 具體壓縮方法,將給定文件添加進壓縮文件中,並處理壓縮文件中的路徑
* @param file 給定磁碟文件(是文件直接添加,是目錄遞歸調用添加)
* @param encrypter AesZipFileEncrypter實例,用於輸出加密ZIP文件
* @param pathForEntry ZIP文件中的路徑
* @param passwd 壓縮密碼
* @throws IOException
*/
private static void doZip(File file, AesZipFileEncrypter encrypter,
String pathForEntry, String passwd) throws IOException {
if (file.isFile()) {
pathForEntry += file.getName();
encrypter.add(file, pathForEntry, passwd);
return;
}
pathForEntry += file.getName() + File.separator;
for(File subFile : file.listFiles()) {
doZip(subFile, encrypter, pathForEntry, passwd);
}
}
/**
* 使用給定密碼解壓指定壓縮文件到指定目錄
* @param inFile 指定Zip文件
* @param outDir 解壓目錄
* @param passwd 解壓密碼
*/
public static void unzip(String inFile, String outDir, String passwd) {
File outDirectory = new File(outDir);
if (!outDirectory.exists()) {
outDirectory.mkdir();
}
AESDecrypter decrypter = new AESDecrypterBC();
AesZipFileDecrypter zipDecrypter = null;
try {
zipDecrypter = new AesZipFileDecrypter(new File(inFile), decrypter);
AesZipFileDecrypter.charset = "utf-8";
/**
* 得到ZIP文件中所有Entry,但此處好像與JDK里不同,目錄不視為Entry
* 需要創建文件夾,entry.isDirectory()方法同樣不適用,不知道是不是自己使用錯誤
* 處理文件夾問題處理可能不太好
*/
List<ExtZipEntry> entryList = zipDecrypter.getEntryList();
for(ExtZipEntry entry : entryList) {
String eName = entry.getName();
String dir = eName.substring(0, eName.lastIndexOf(File.separator) + 1);
File extractDir = new File(outDir, dir);
if (!extractDir.exists()) {
FileUtils.forceMkdir(extractDir);
}
/**
* 抽出文件
*/
File extractFile = new File(outDir + File.separator + eName);
zipDecrypter.extractEntry(entry, extractFile, passwd);
}
} catch (IOException e) {
e.printStackTrace();
} catch (DataFormatException e) {
e.printStackTrace();
} finally {
try {
zipDecrypter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 測試
* @param args
*/
public static void main(String[] args) {
/**
* 壓縮測試
* 可以傳文件或者目錄
*/
// zip("M:\\ZIP\\test\\bb\\a\\t.txt", "M:\\ZIP\\test\\temp1.zip", "zyh");
// zip("M:\\ZIP\\test\\bb", "M:\\ZIP\\test\\temp2.zip", "zyh");
unzip("M:\\ZIP\\test\\temp2.zip", "M:\\ZIP\\test\\temp", "zyh");
}
}
壓縮多個文件時,有兩個方法(第一種沒試):
(1) 預先把多個文件壓縮成zip,然後調用enc.addAll(inZipFile, password);方法將多個zip文件加進來。
(2)針對需要壓縮的文件循環調用enc.add(inFile, password);,每次都用相同的密碼。
㈥ java導出excel報表顯示使用了不支持的壓縮格式怎麼解決
字元編碼推薦使用UTF-8,如果想壓縮,javaIO中內置ZIP格式的壓縮,你需要先把EXCEL文件生存在臨時文件夾中,然後通過ZIP進行壓縮,但java的壓縮對中文支持不是很好,如果文件名是中文會有亂碼,壓縮之後再導出就可以了!
㈦ 如何用JAVA 壓縮POI生成的EXCEL
看出錯信來息,需要用XSSF處理源,是你讀取的文件格式不對吧,獲取的file是XML文件嗎?可以加一句System.out.pringtln(file.getName())看看,poi是處理excel的。
㈧ 如何將excel轉換成zip格式
用壓縮軟體將excel進行壓縮,壓縮格式選擇zip就可以了。
excel自身就是多個文件,可以用7-z壓縮軟體將excel或word文件打開,就可以看到裡面有多個文件。不要更改裡面的文件,不然office文件可能會損壞。
㈨ java 壓縮excel後 excel變成空的了
首先創建文件目錄,然後生成Excel文件到創建的目錄下,
通過IO流壓縮Excel文件成zip文件 到指定目錄,最後刪除指定目錄下所有的Excel文件。
package pack.java.io.demo;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
/**
* zip壓縮文件實例
* add by 周海濤
* @author Administrator
*
*/
public class ZipDemo {
/**
* @param args
* @throws IOException
* @throws WriteException
* @throws RowsExceededException
*/
public static void main(String[] args) throws RowsExceededException, WriteException, IOException {
String path = "C:/document/excel";
//創建文件夾;
createFile(path);
//創建Excel文件;
createExcelFile(path);
//生成.zip文件;
craeteZipPath(path);
//刪除目錄下所有的文件;
File file = new File(path);
//刪除文件;
deleteExcelPath(file);
//重新創建文件;
file.mkdirs();
}
/**
* 創建文件夾;
* @param path
* @return
*/
public static String createFile(String path){
File file = new File(path);
//判斷文件是否存在;
if(!file.exists()){
//創建文件;
boolean bol = file.mkdirs();
if(bol){
System.out.println(path+" 路徑創建成功!");
}else{
System.out.println(path+" 路徑創建失敗!");
}
}else{
System.out.println(path+" 文件已經存在!");
}
return path;
}
/**
* 在指定目錄下創建Excel文件;
* @param path
* @throws IOException
* @throws WriteException
* @throws RowsExceededException
*/
public static void createExcelFile(String path) throws IOException, RowsExceededException, WriteException{
for(int i =0;i<3;i++){
//創建Excel;
WritableWorkbook workbook = Workbook.createWorkbook(new File(path+"/" + new SimpleDateFormat("yyyyMMddHHmmsss").format(new Date() )+"_"+(i+1)+".xls"));
//創建第一個sheet文件;
WritableSheet sheet = workbook.createSheet("導出Excel文件", 0);
//設置默認寬度;
sheet.getSettings().setDefaultColumnWidth(30);
//設置字體;
WritableFont font1 = new WritableFont(WritableFont.ARIAL,14,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.RED);
WritableCellFormat cellFormat1 = new WritableCellFormat(font1);
//設置背景顏色;
cellFormat1.setBackground(Colour.BLUE_GREY);
//設置邊框;
cellFormat1.setBorder(Border.ALL, BorderLineStyle.DASH_DOT);
//設置自動換行;
cellFormat1.setWrap(true);
//設置文字居中對齊方式;
cellFormat1.setAlignment(Alignment.CENTRE);
//設置垂直居中;
cellFormat1.setVerticalAlignment(VerticalAlignment.CENTRE);
//創建單元格
Label label1 = new Label(0, 0, "第一行第一個單元格(測試是否自動換行!)",cellFormat1);
Label label2 = new Label(1, 0, "第一行第二個單元格",cellFormat1);
Label label3 = new Label(2, 0, "第一行第三個單元格",cellFormat1);
Label label4 = new Label(3, 0, "第一行第四個單元格",cellFormat1);
//添加到行中;
sheet.addCell(label1);
sheet.addCell(label2);
sheet.addCell(label3);
sheet.addCell(label4);
//給第二行設置背景、字體顏色、對齊方式等等;
WritableFont font2 = new WritableFont(WritableFont.ARIAL,14,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.BLUE2);
WritableCellFormat cellFormat2 = new WritableCellFormat(font2);
cellFormat2.setAlignment(Alignment.CENTRE);
cellFormat2.setBackground(Colour.PINK);
cellFormat2.setBorder(Border.ALL, BorderLineStyle.THIN);
cellFormat2.setWrap(true);
//創建單元格;
Label label11= new Label(0, 1, "第二行第一個單元格(測試是否自動換行!)",cellFormat2);
Label label22 = new Label(1, 1, "第二行第二個單元格",cellFormat2);
Label label33 = new Label(2, 1, "第二行第三個單元格",cellFormat2);
Label label44 = new Label(3, 1, "第二行第四個單元格",cellFormat2);
sheet.addCell(label11);
sheet.addCell(label22);
sheet.addCell(label33);
sheet.addCell(label44);
//寫入Excel表格中;
workbook.write();
//關閉流;
workbook.close();
}
}
/**
* 生成.zip文件;
* @param path
* @throws IOException
*/
public static void craeteZipPath(String path) throws IOException{
ZipOutputStream zipOutputStream = null;
File file = new File(path+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".zip");
zipOutputStream = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
File[] files = new File(path).listFiles();
FileInputStream fileInputStream = null;
byte[] buf = new byte[1024];
int len = 0;
if(files!=null && files.length > 0){
for(File excelFile:files){
String fileName = excelFile.getName();
fileInputStream = new FileInputStream(excelFile);
//放入壓縮zip包中;
zipOutputStream.putNextEntry(new ZipEntry(path + "/"+fileName));
//讀取文件;
while((len=fileInputStream.read(buf)) >0){
zipOutputStream.write(buf, 0, len);
}
//關閉;
zipOutputStream.closeEntry();
if(fileInputStream != null){
fileInputStream.close();
}
}
}
if(zipOutputStream !=null){
zipOutputStream.close();
}
}
/**
* 刪除目錄下所有的文件;
* @param path
*/
public static boolean deleteExcelPath(File file){
String[] files = null;
if(file != null){
files = file.list();
}
if(file.isDirectory()){
for(int i =0;i<files.length;i++){
boolean bol = deleteExcelPath(new File(file,files[i]));
if(bol){
System.out.println("刪除成功!");
}else{
System.out.println("刪除失敗!");
}
}
}
return file.delete();
}
}