在asp.net中实现dataset与excel的相互导入导出

来源:互联网 发布:qq飞车圣殿骑士数据 编辑:程序博客网 时间:2024/05/01 10:04

从excel文件导入到dataset可以把excel文件看作是一个数据库,因此这一部分其实就是一个从数据库取数然后保存在dataset的过程,相对比较简单。 代码如下

public DataSet Import(System.Web.UI.HtmlControls.HtmlInputFile SelectFile,DataSet private_DataSet,DataGrid dg,int fromcolumn)
  {
   int theplace=0;
   string source="";
   string selectfile="";
   DataSet objDataset1=new DataSet(); 

   if ( SelectFile.PostedFile.FileName != "" )
   {
    string path=getPath();
    string fileName = System.IO.Path.GetFileName( SelectFile.PostedFile.FileName );  //返回本地指定路径字符串的文件名和扩展名

    selectfile = System.IO.Path.Combine(path,fileName); 
    //以跨平台方式(Path),合并两个路径字符串——即Web服务器上的指定虚拟路径相对应的物理文件路径、本地指定路径字符串的文件名和扩展名
    SelectFile.PostedFile.SaveAs( selectfile);  //保存上载文件的内容
   }

   //创建一个数据链接
   string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source="+selectfile +";" + "Extended Properties=Excel 8.0;";
   OleDbConnection myConn = new OleDbConnection ( strCon ) ;
   string strCom = " SELECT * FROM [Sheet1$] " ;
   myConn.Open ( ) ;
   //打开数据链接,得到一个数据集
   OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
   //得到自己的DataSet对象
   myCommand.Fill ( objDataset1 , "[Sheet1$]" ) ;
   //关闭此数据链接
   myConn.Close ( ) ;

   
   private_DataSet.Tables[0].Rows.Clear();
   for (int i=0;i<objDataset1.Tables[0].Rows.Count;i++)
   {
    private_DataSet.Tables[0].Rows.Add(private_DataSet.Tables[0].NewRow());
    //int temp=2;
    int temp=fromcolumn;
    for (int j=0;j<objDataset1.Tables[0].Columns.Count;j++)
    {
     //     if(dg.Columns[temp].Visible==true)
     //     {
     BoundColumn s=(BoundColumn)dg.Columns[temp];
     source=s.DataField.ToString();
     theplace=private_DataSet.Tables[0].Columns.IndexOf(source);
 
     string aa=private_DataSet.Tables[0].Columns[theplace].DataType.ToString();
  
     string zhi=objDataset1.Tables[0].Rows[i][j].ToString();
     switch(aa)
     {
      case "System.Char":
       private_DataSet.Tables[0].Rows[i][theplace]=zhi;
       break;
      case "System.String":
       private_DataSet.Tables[0].Rows[i][theplace]=zhi;
       break;
      case "System.Int32":
       if ((zhi!="")&&(zhi!=null))
        private_DataSet.Tables[0].Rows[i][theplace]=Convert.ToInt32(zhi);
       break;
      case "System.Decimal":
       if ((zhi!="")&&(zhi!=null))
        private_DataSet.Tables[0].Rows[i][theplace]= Convert.ToDecimal(zhi);
       break;
      case "System.DateTime":
       if ((zhi!="")&&(zhi!=null))
        private_DataSet.Tables[0].Rows[i][theplace]=Convert.ToDateTime(zhi);
       break;
     }
     temp++;
    }
   } 

   return private_DataSet;

  }

将dataset的数据写入excel文件,首先要启动excel进程,然后逐行将dataset中的数据写入到启动的excel文件中,最后提示下载。并退出进程。代码如下

/**//// <summary>
  ///导出到 Excel 并提示下载
  /// </summary>
  public void ExportExcel(DataSet my_Ds,string filename)
  {
   Excel.Application  oExcel;
   oExcel  =  new  Excel.Application();
   try
   {
    
    Excel.Workbook  oBook; 
    Object  oMissing  =  System.Reflection.Missing.Value; 
    
    oBook =  oExcel .Workbooks.Add(oMissing); 
    HttpResponse response = HttpContext.Current.Response;
   
    int lie=my_Ds.Tables[0].Columns.Count;
    int hang=my_Ds.Tables[0].Rows.Count;
    int i,j,t;
    string panduanstring="";
    i=1;
    for (j=0;j<lie;j++)//标题
    {

     oExcel.Cells[1,i++]=my_Ds.Tables[0].Columns[j].ColumnName;

    }
   
    t=1;
    for  (i=0;i<hang;i++)//内容
    { 
     for (j=0;j<lie;j++)
     {

      //     BoundColumn s=(BoundColumn)dg.Columns[j];
      //     source=s.DataField.ToString();
      //     theplace=my_Ds.Tables[0].Columns.IndexOf(source);
      //     panduanstring=my_Ds.Tables[0].Rows[i][theplace].ToString();
      panduanstring=my_Ds.Tables[0].Rows[i][j].ToString();
      if (panduanstring.GetType().ToString()=="System.String")
      {
       oExcel.Cells[i+2,t++]="'"+my_Ds.Tables[0].Rows[i][j].ToString();
      }
      else
       oExcel.Cells[i+2,t++]=my_Ds.Tables[0].Rows[i][j].ToString();

     }
     t=1;
    }

    oExcel.Visible=true;
    oBook.Saved  =  true; 
    oExcel.UserControl  =  false; 

    string path=getPath();
    string  mm=path+filename+".xls";

    oExcel.ActiveWorkbook.SaveCopyAs(mm);
    oExcel.Quit();
    System.Runtime.InteropServices.Marshal.ReleaseComObject((object)oExcel);
    GC.Collect();
    response.Redirect(filename+".xls");
   }
   
   catch
   {
    oExcel.Quit();
    System.Runtime.InteropServices.Marshal.ReleaseComObject((object)oExcel);
    GC.Collect();
   }
  }

 

原创粉丝点击