乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>java</em>中修改zip压缩文件中的excel文件的内容

<em>java</em>中修改zip压缩文件中的excel文件的内容

作者:乔山办公网日期:

返回目录:excel表格制作


你只是给A列添加值,B列为什么会变?POI只是读写流操作数据.至于公式变化那是EXCEL自带的功能,你POI操作的是EXCEL文档而不是EXCEL本身,这个你得弄清楚
要想达到同样的效果,得到公式,然后根据参数计算出结果塞进B列中就行

至于你要操作EXCEL可以用JACOB,那是操作的EXCEL

main里只有输入流,将数据读取到内存,改的是内存的值,没有用输出流写到文件中!
两个方法合到一块试试,如果再不行的话就先解压,改完再压缩!
用这个方式占用内存比e799bee5baa6e59b9ee7ad94362较小,不过不能控制excel格式
public class Excel {
public static void main(String[] args) {
try {
String url = "jdbc:oracle:thin:@10.20.1.23:1521:ilisdev";
String username = "uu";
String password = "pp";
Connection conn;
Statement stmt;
ResultSet rs;
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, username, password);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
String sql = "select a.appntname,c.riskname,b.name from lcpol a,ldcom b,lmrisk c "
+ "where a.managecom=b.comcode and a.riskcode = c.riskcode";
rs = stmt.executeQuery(sql);

String path = "c:/test.txt";// 数据存放的位置
Excel excel = new Excel(path, new String[] );

excel.makeXLS(rs, 3);
} catch (Exception e) {
e.printStackTrace();
}
}

private BufferedWriter buff = null;

public Excel(String filePath, String[] titles) {
try {
if (filePath == null || filePath.equals("") || titles == null
|| titles.length == 0) {
System.out.println("路径或标题为空!");
} else {
buff = new BufferedWriter(new FileWriter(filePath));
for (int i = 0; i < titles.length; i++) {
buff.write(titles[i] + "\t");
if (i == (titles.length - 1))
buff.write("\r\n");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

public void makeXLS(ResultSet rs, int colNum) {
try {
while (rs.next()) {
for (int i = 1; i <= colNum; i++) {
buff.write(rs.getString(i) + "\t");
if (i == colNum) {
buff.write("\r\n");
}
}
}
buff.close();
} catch (Exception e) {
e.printStackTrace();
}

}
}


首先创建文件目录,然后生成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;
}

/**
* 在指e799bee5baa6e58685e5aeb9334定目录下创建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();
}
}

相关阅读

  • java怎么创建新文件到webroot文件下

  • 乔山办公网excel表格制作
  • 放在webContent或者叫webRoot下,部署的时候这个文件夹会放到服务器上如果用工具,那就鼠标来点到webroot,右键,新建文件夹即可源,如果使用程序,则为String savePath = this.getServletContext
关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网