CSDN博客

img softwave
博客专家

POI读取Excel文件的带注释小例子

发表于2008/9/29 18:56:00  2450人阅读

分类: Java体系

欢迎点击此处订阅本Blog Blog 订阅

<script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; google_ad_width = 468; google_ad_height = 60; google_ad_format = "468x60_as"; google_ad_type = "image"; //2007-07-26: CSDN google_ad_channel = "6063905817"; google_color_border = "6699CC"; google_color_bg = "E6E6E6"; google_color_link = "FFFFFF"; google_color_text = "333333"; google_color_url = "AECCEB"; google_ui_features = "rc:6"; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

本文转自:http://www.blogjava.net/jwin/archive/2007/12/11/167093.html

试试用POI操作excel囖.
纯粹为了自己好找.
要有人看了这些的话,随意就好啦...欢迎指出不当之处.
package xls;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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

public class ReadXL
{
    
public static String fileName = "C://xls//EXCEL.xls";
    
public static String path = "C://xls//"//为方便,就这样写了.
    public static void main(String argv[])
    
{
        List list 
= readExcel();
        String xmldata 
= buildXML(list);
        createXMLFile(xmldata);
    }


    
/**
     * 读取磁盘上的EXCEL文件的内容
     * 
@return List
     
*/

    
public static List readExcel()
    
{
        List list 
= null;
        UserBean ub 
= null;
        
try
        
{
            HSSFWorkbook workbook 
= new HSSFWorkbook(new FileInputStream(
                    fileName)); 
//得到excel对象
            HSSFSheet sheet = workbook.getSheetAt(0); //得到第一个sheet
            int rows = sheet.getPhysicalNumberOfRows(); //得到行数
            list = new ArrayList();
            
for (int i = 1; i < rows; i++)
            
{
                HSSFRow row 
= sheet.getRow(i);
                ub 
= new UserBean();
                HSSFCell cell 
= row.getCell((short0); //得到列0(下标0,为第一列)
                ub.setName(cell.getStringCellValue());
                cell 
= row.getCell((short1); //得到列 1
                ub.setEmail(cell.getStringCellValue());
                cell 
= row.getCell((short2); //得到列2
                ub.setPhone(cell.getStringCellValue());
                cell 
= row.getCell((short3); //得到列3
                ub.setPasswd(cell.getStringCellValue());
                list.add(ub);
            }

            
return list;
        }

        
catch (Exception e)
        
{
            
return null;
        }

    }


    
/**
     * 组装xml格式字符串
     * 
@param list List
     * 
@return String
     
*/

    
public static String buildXML(List list)
    
{
        StringBuffer sb 
= new StringBuffer();
        sb.append(
"<contents-list>/n/t");
        sb.append(
"<content>/n/t/t");
        
for (int i = 0; i < list.size(); i++)
        
{
            UserBean bean 
= (UserBean) list.get(i);
            sb.append(
"<userinfo>/n/t/t/t");
            sb.append(
"<name>" + bean.getName() + "</name>/n/t/t/t");
            sb.append(
"<email>" + bean.getEmail() + "</email>/n/t/t/t");
            sb.append(
"<phone>" + bean.getPhone() + "</phone>/n/t/t/t");
            sb.append(
"<passwd>" + bean.getPasswd() + "</passwd>/n/t/t");
            
if (i + 1 < list.size())
            
{
                sb.append(
"</userinfo>/n/t/t");
            }

            
else
            
{
                sb.append(
"</userinfo>/n/t");
            }

        }

        sb.append(
"</content>/n");
        sb.append(
"</contents-list>");
        
return sb.toString();
    }



    
/**
     * 输出到文件
     * 
@param xmldata String
     
*/

    
public static void createXMLFile(String xmldata)
    
{
        String createTime 
= createTime();
        String filename 
= path + createTime + ".xml";
        OutputStreamWriter osw 
= null;
        FileOutputStream output 
= null;
        
try
        
{
            output 
= new FileOutputStream(filename);
            osw 
= new OutputStreamWriter(output, "utf-8");
            osw.write(xmldata);
            osw.flush();
        }

        
catch (Exception ex)
        
{
        }
        
        
finally
        
{
            
try
            
{
                
if (null != output)
                
{
                    output.close();
                }

            }

            
catch (IOException ex2)
            
{
            }

            
try
            
{
                
if (null != osw)
                
{
                    osw.close();
                }

            }

            
catch (IOException ex3)
            
{
            }

        }

    }


    
/**
     * 用时间作为文件名
     * 
@return String
     
*/

    
public static String createTime()
    
{
        Date date 
= new Date();
        SimpleDateFormat dateFormat 
= new SimpleDateFormat("yyyyMMddmmss");
        String createTime 
= dateFormat.format(date);
        
return createTime;
    }


}


再就一个UserBean,就几个属性.

<script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; /* 468x60, 创建于 08-8-6 */ google_ad_slot = "7368701459"; google_ad_width = 468; google_ad_height = 60; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

<script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; /* 468x15 横链接单元 */ google_ad_slot = "5785741422"; google_ad_width = 468; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

<script type="text/javascript" src="http://www.google.com/reader/ui/publisher.js"></script> <script type="text/javascript" src="http://www.google.com/reader/public/javascript/user/00697638153916680411/state/com.google/broadcast?n=5&callback=GRC_p(%7Bc%3A%22green%22%2Ct%3A%22%5Cu8FD9%5Cu4E9B%5Cu6587%5Cu7AE0%5Cu4E5F%5Cu503C%5Cu5F97%5Cu4E00%5Cu770B%22%2Cs%3A%22false%22%7D)%3Bnew%20GRC"></script>
0 0

相关博文

我的热门文章

img
取 消
img