文本文件信息导入Excel中(NPOI方式,只提取公司名称、手机号码)

文本文件格式:
  1   山东华德城市投资有限公司   13793695297  
 
2   山东钢联物流有限公司   8901343  
 
3   山东潍坊福田模具有限责任公司   0536 - 7608638  
 
4   潍坊市临朐矿兴轻钙股份有限公司   3610649  
 
5   潍坊金宝新型建材有限公司   8804346  
 
6   潍坊中外运仓储物流有限公司   053282897780   13805326133  

实现代码:

ExpandedBlockStart.gif 代码
  protected   void  btnBegin_Click( object  sender, EventArgs e)
    {
        StreamReader sr 
=   new  StreamReader( @" F:\\ComTel.txt " , System.Text.Encoding.GetEncoding( " gb2312 " ));
        HSSFWorkbook workbook 
=   new  HSSFWorkbook();
        HSSFSheet sheet 
=  workbook.CreateSheet( " 企业联系方式 " );
        HSSFRow row 
=  sheet.CreateRow( 0 );
        row.CreateCell(
0 ).SetCellValue( " 序号 " );
        row.CreateCell(
1 ).SetCellValue( " 公司名称 " );
        row.CreateCell(
2 ).SetCellValue( " 联系方式 " );
        
int  i  =   1 ;
        
while  ( ! sr.EndOfStream)
        {
            
string  str  =  sr.ReadLine();
            
string [] lines  =  str.Trim().Split( new   char [] {  ' \n ' ' \r '  }, StringSplitOptions.RemoveEmptyEntries);

            
foreach  ( string  line  in  lines)
            {
                
string [] data  =  line.Split( new   string [] {  "    " "   "  }, StringSplitOptions.RemoveEmptyEntries);
                
string  mobile  =   string .Empty;
                
string  num  =  data[ 0 ];
                
string  name  =  data[ 1 ];
                
try
                {
                    
if  (data[ 2 ].Length  ==   11 )
                        mobile 
=  data[ 2 ];
                    
else
                    {
                        
try
                        {
                            
if  (data[ 3 ].Length  ==   11 )
                                mobile 
=  data[ 3 ];
                        }
                        
catch
                        {
                            
continue ;
                        }
                    }
                }
                
catch  (Exception)
                {
                    
continue ;
                }
                HSSFRow newRow 
=  sheet.CreateRow(i);
                newRow.CreateCell(
0 , HSSFCell.CELL_TYPE_STRING).SetCellValue(i.ToString());
                newRow.CreateCell(
1 , HSSFCell.CELL_TYPE_STRING).SetCellValue(name);
                newRow.CreateCell(
2 , HSSFCell.CELL_TYPE_STRING).SetCellValue(mobile);
                i
++ ;
            }
        }

        sr.Close();
        
string  filename  =   @" F:\\公司联系方式.xls " ;
        
using  (FileStream stream  =   new  FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite))
        {
            workbook.Write(stream);
        }
        MessageBox.Show(
this .Page,  " 导出完毕 " );
        System.Diagnostics.Process.Start(filename);
// 自动打开Excel文件
    }

转载于:https://www.cnblogs.com/caoshuai/archive/2010/10/25/1860640.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值