各位老大.......................救命啊~~~~~~~~~
告诉我一段能够实现导入EXCL 表格的代码把...谢谢拉~~
急啊...5555555555555555

解决方案 »

  1.   

    一个办法    public bool doImportContacts(string fp){
          bool ret=functi.FALSE;
          Excel.Application excel = new Excel.Application();  // 引用Excel对象
          Excel._Workbook wb=excel.Application.Workbooks.Open(fp, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
          excel.Visible = functi.FALSE ;    // 使 Excel NOT 可视    
          
          int row;      string cntName, cntMobile      //mobile, name
          foreach(Excel._Worksheet ws in wb.Worksheets){        for(row=1;row<1000;row++){
              try{
                cntMobile=((Excel.Range)ws.Cells[row, 1]).Value.ToString();
                cntName=((Excel.Range)ws.Cells[row, 2]).Value.ToString();            contact.AddContact(cntName, cntOwner);
                ret=functi.TRUE;
              }catch{
              }
            }//for(row=1      }//foreach      wb.Close(functi.FALSE, missing, missing);
          excel.Quit();      return ret;
        }
      
      

  2.   

    //导入资料
    public static bool ImportDataFromExcel(string filePath,System.Data.DataTable dt,string voucherNo,System.Windows.Forms.ProgressBar pbBar,out string msg)
    {
    try
    {
    string tempReceiptNo = setupDao.GetSetup().Rows[0]["ReceiptNo"].ToString();
    Excel.Application xlsApp = new Excel.Application();
        object missing = System.Reflection.Missing.Value;
    Excel.Workbooks wbs = xlsApp.Workbooks;
    Excel.Workbook wb = wbs.Open(filePath,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing);
    xlsApp.Visible  = false;  //使Excel不可视
    Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];
    dt.Clear();
    //int pbStep = 0;
    if (pbBar != null)
    {
    pbBar.Value = 0;
    pbBar.Step = pbBar.Maximum / ws.Rows.Count + 1;
    }
    for(int i = 2;i< ws.Rows.Count;i++)
    {
    if(((Excel.Range)ws.Cells[i,1]).Text.ToString() != string.Empty)
    {
    DataRow row = dt.NewRow();
    row["VoucherNo"] = voucherNo;
    row["ReceiptNo"] = GetNewReceiptNo(tempReceiptNo);
    tempReceiptNo = (string)row["ReceiptNo"];
    row["ReceivedFromDesc"] = ((Excel.Range)ws.Cells[i,1]).Text;
    row["Sum"]  = ((Excel.Range)ws.Cells[i,2]).Text;
    row["CheckNo"] = ((Excel.Range)ws.Cells[i,3]).Text;
    row["Cash"] = ((Excel.Range)ws.Cells[i,4]).Text;
    dt.Rows.Add(row);
    pbBar.PerformStep();
    }
    else
    {
    pbBar.Value = pbBar.Maximum;
    break;
    }
    }
    wbs.Close();
    xlsApp.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(wbs);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsApp);
    msg = "Import data sucessfully!";
    return true;
    }
    catch(Exception ex)
    {
    Console.WriteLine(ex.ToString());
    msg = ex.ToString();
    return false;
    }
    finally
    {
    GC.Collect();
    GC.WaitForPendingFinalizers();
    }
    }
      

  3.   

    请问高手:实现导入EXCEL 表格的具体过程是怎样的呀?还有,导出EXCEL 表格又是怎样实现的呢?我是个新手,呵呵,麻烦讲的稍微详细一点哦~