「javaexcel压缩」excel压缩数据
本篇文章给大家谈谈javaexcel压缩,以及excel压缩数据对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、怎样把excel文件压缩变小
- 2、如何用JAVA 压缩POI生成的EXCEL
- 3、java 压缩excel后 excel变成空的了
- 4、java中修改zip压缩文件中的excel文件的内容。
- 5、如何把EXCEL文件压缩到更小?
- 6、java :怎么实现分批次导出excel的条数限制,每次导出的excel压缩成zip,并且导出时给用户添加友好提示?
怎样把excel文件压缩变小
方法:
1、一般excel文件太大是因为内部包含太多图片,可以将图片压缩,首先打开excel。
2、找到图片并右键单击,选择“大小和属性”,设置完图片大小点击“文件”——“另存为”。
3、在“另存为”页面选择“工具”,点击“压缩图片”,根据需要选择分辨率,点击“保存”即可完成操作。
excel使用技巧:
1、excel可以只打印表格中的一部分,首先打开一个Excel表格,将需要打印的部分内容复制,然后新建一个excel表格,将内容复制到新的表格,最后打印即可。
2、点击菜单栏中的“数据”,选择“排序和筛选”,点击“高级”,可以查看不重复数据。
如何用JAVA 压缩POI生成的EXCEL
看出错信息,需要用XSSF处理,是你读取的文件格式不对吧,获取的file是XML文件吗?可以加一句System.out.pringtln(file.getName())看看,poi是处理excel的。
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;i3;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;ifiles.length;i++){
boolean bol = deleteExcelPath(new File(file,files[i]));
if(bol){
System.out.println("删除成功!");
}else{
System.out.println("删除失败!");
}
}
}
return file.delete();
}
}
java中修改zip压缩文件中的excel文件的内容。
main里只有输入流,将数据读取到内存,改的是内存的值,没有用输出流写到文件中!
两个方法合到一块试试,如果再不行的话就先解压,改完再压缩!
如何把EXCEL文件压缩到更小?
工具/原料
EXCEL丶EXCEL文件
步骤
1.先打开需要压缩的包含大量图片的EXCEL文件;
2.打开后先设定图片所需要的大小;
3.图片大小设定完成后点击最左上角的“文件”按钮;
4.从弹出的菜单中选择“另存为”命令,并选择保存位置;
5.在弹出的“另存为”对话框中选择“工具”,再选择“压缩图片”;
6.在弹出的“压缩图片”对话框中选择输出分辨率,如果要压缩更小,则可以选择更底的分辨率,而如果还要最大化压缩,则可以再选择“删除图片的剪裁区域”,设置完成后点击“确定”按钮;
7.然后在返回的“另存为”对话框中选择“保存”按钮,并选择“是”即可。
java :怎么实现分批次导出excel的条数限制,每次导出的excel压缩成zip,并且导出时给用户添加友好提示?
你这问题太大了
HSSFWorkbook 是导出excel的工具,可是实现导出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 ; icolsAddSize ; i++){
cell = row.createCell((short)i);
cell.setCellValue(NewmasterstudentAction.COLS4EXPORT_CODE[i]+"("+NewmasterstudentAction.COLS4EXPORT_NAME[i]+")");
}
for(int i=0;ijobList.size();i++){
UserJobColumn userJob = jobList.get(i);
jobColumn = userJob.getJobColumn();
cell = row.createCell((short)(i+colsAddSize));
cell.setCellValue(jobColumn.getColumnCode()+"("+jobColumn.getColumnName()+")"); //字段代码
}
//打印记录
SetEntryString,ListJobRecord entrySet = des.entrySet();
IteratorEntryString, ListJobRecord it = entrySet.iterator();
int index = 1;
while(it.hasNext()){
EntryString, ListJobRecord 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());
}
//打印记录
ListJobRecord 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();
javaexcel压缩的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于excel压缩数据、javaexcel压缩的信息别忘了在本站进行查找喔。