;

在C/S中数据导入Excel

private void btn_Export_Click(object sender, System.EventArgs e)
  {
   try
   {
  
    // 添加进度条
    System.Windows.Forms.Form  form = new Form();
    System.Windows.Forms.ProgressBar progressBar3 = new ProgressBar();
    form.Width = 200;
    form.Height = 20;            
    progressBar3.Width = 200;
    progressBar3.Height =20;                        
    form.ControlBox = false;       
    form.Enabled = true;                     
    progressBar3.Parent = form;
    form.StartPosition  = FormStartPosition.CenterScreen ;
    progressBar3.Minimum = 0;
    progressBar3.Maximum = 100;             
    progressBar3.Visible = true;
    form.Visible = true;
    
    Excel.ApplicationClass excelKccx= new  Excel.ApplicationClass();//创建excel对象         
    excelKccx.Workbooks.Add(true);//创建excel工作薄 
    
    try
    {  
     System.Data.DataTable myDataTable=Excelds.Tables[0];//创建一个数据表,得到DataSet中“库存信息”表中的数据 Excelds 为自定义的DataSet用于记录要导入到Excel的数据
     int row=2;
     //把数据表的各个信息输入到excel表中
     //    for(int i=0;i<=75;i++)//取字段名
     //    {
     if(this.chkb_sb.Checked)
     {
      #region 生成的样式 
      int I=1;
      excelKccx.Cells[1,I++]="xh";//为Excel中最上面的字段头(每一列的头)
      excelKccx.Cells[1,I++]="ksh";
      excelKccx.Cells[1,I++]="xxdm";
      excelKccx.Cells[1,I++]="xxmc";
      excelKccx.Cells[1,I++]="xxszd";
      excelKccx.Cells[1,I++]="xm";
      excelKccx.Cells[1,I++]="cym";
      excelKccx.Cells[1,I++]="xb";
     
      #endregion
      excelKccx.Cells.NumberFormat ="@";//设置Excel为文本形的
      excelKccx.Cells.ColumnWidth="15";//设置列每一列的宽度
      for(int i=0;i<myDataTable.Rows.Count;i++ )//取记录值
      {
        
         excelKccx.Cells[row,1]=myDataTable.Rows[i]["xs_dm"].ToString();
         excelKccx.Cells[row,2]=myDataTable.Rows[i]["xs_kh"].ToString();
         excelKccx.Cells[row,6]=myDataTable.Rows[i]["xs_xm"].ToString();
         excelKccx.Cells[row,8]=myDataTable.Rows[i]["xs_xb"].ToString();
         excelKccx.Cells[row,9]=myDataTable.Rows[i]["cs_rq"].ToString();
         excelKccx.Cells[row,10]=myDataTable.Rows[i]["xs_mz"].ToString();       
    
     
       progressBar3.Value  = (i*100)/myDataTable.Rows.Count;
         
       if(i==myDataTable.Rows.Count-1) 
       {
        form.Close();
       }
       row++; 
     
      }
     }
     }
    catch(Exception ex)
    {
     MessageBox.Show(ex.Message);
    }
    finally
    {
     form.Close();
     excelKccx.Visible=true;//使excel可见*/       
    
    }
   }
  
   catch(Exception ex)
   {
    MessageBox.Show(ex.Message);
   }
  }
 }

posted @ 2006-08-14 18:14  Ж╰ァ流星  阅读(541)  评论(0编辑  收藏  举报