利用POI创建OpenOffice中的Excel文件

本文介绍了一种使用Java POI库生成Excel文件的方法,并提供了一个简单的示例代码。该方法适用于将数据导出到Excel表格中,特别是对于需要兼容旧版MS Office软件的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之所以称为OpenOffice的Excel文件,我发现了一个特点就是:
一些网站严格限定了文件必须为MS的Excel格式的话,用POI的HSSF创建的Excel就会不识别.不知道是什么原因,可能是版本的问题,据说HSSF(令人讨厌的电子表格格式)生成的是MS97的格式.但是97-2003的提示中明显的说明了MS的lib可以读取的啊.搞不懂这是为什么.

以下是Java的代码:

处理类:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

import org.apache.poi.hssf.usermodel.*;

/**
 * 
@author qxhuang
 *
 
*/
public class ExportToExcel {
private final static List<String[]> items=new ArrayList<String[]>();

/**
 * Init Datas
 * 
@param Address
 * 
@param Zip
 
*/
public static void GenereateItem(String Address,String Zip){
    items.add(
new String[]{Address,Zip});
}
/**
 * Export To Excel
 * 
@param filepath
 
*/
public static void ExportExcel(String filepath)throws IOException{
    HSSFWorkbook workbook
=new HSSFWorkbook();
    HSSFDataFormat format
=workbook.createDataFormat();
    HSSFSheet sheet
= workbook.createSheet("sheet1");
    HSSFRow row
=sheet.createRow((short)0);
    
//add head cell
    HSSFCell cell= row.createCell((short)0);
    cell.setCellValue(
new HSSFRichTextString("Address"));
    
    cell
= row.createCell((short)1);
    cell.setCellValue(
new HSSFRichTextString("Zip"));
    
    
if(items.size()>0){
        
for(int i=0;i<items.size();i++){
            HSSFRow newrow
=sheet.createRow(i+1);
            String[] item
=(String[])items.get(i);
            
for(int x=0;x<item.length;x++){
                HSSFCell newcell
=newrow.createCell((short)x);
                newcell.setCellValue(
new HSSFRichTextString(item[x].toString()));
            }
        }
    }
    
for(int i=0;i<=items.size();i++){
        sheet.autoSizeColumn((
short)i);
    }
    FileOutputStream file
=null;
    
try{
        file
=new FileOutputStream(filepath);
        workbook.write(file);
        file.close();
    }
catch(IOException ex){
        workbook.write(file);
        file.close();
        ex.printStackTrace();
    }
    }
}

 

主函数:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
import java.io.*;
import java.util.*;

/**
 * 
 * 
@author qxhuang
 * 
 
*/
public class Main {
    
static BufferedReader input = new BufferedReader(new InputStreamReader(
            System.in));
    
static String s = "";

    
public static void main(String[] args) throws IOException {
        
while (!s.toLowerCase().equals("done")) {
            s 
= input.readLine();
            
if (s.indexOf(","> -1) {
                String[] temp 
= s.split(",");
                
if (temp.length == 2) {
                    ExportToExcel.GenereateItem(temp[
0], temp[1]);
                }
            }
        }
        
if (s.toLowerCase().equals("done")) {
            File file 
= new File(".");
            String path 
= file.getAbsolutePath();
            String filename 
= path
                    
+ new Date().toString().replace(" """).replace(":""")
                    
+ ".xls";
            
try {
                ExportToExcel.ExportExcel(filename);
                System.out
                        .println(
"Create Complete!! file name is " + filename);
            } 
catch (IOException e) {
                
// TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        input.readLine();
    }

    
/*
     * (non-Java-doc)
     * 
     * @see java.lang.Object#Object()
     
*/
    
public Main() {
        
super();
    }

}

 

所需要的JAR在:

http://poi.apache.org/

转载于:https://www.cnblogs.com/cnherman/archive/2008/09/17/1292637.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值