C# 读excel

 

 

                 
             
#region
               Excel.Application excel = null;
               Excel.Workbooks wbs = null;
               Excel.Workbook wb = null;
               Excel.Worksheet ws = null;
               Excel.Range range1 = null;
               object Nothing = System.Reflection.Missing.Value;

               try
               {
                   excel = new Excel.Application();
                   excel.UserControl = true;
                   excel.DisplayAlerts = false;
                   excel.Application.Workbooks.Open(filename, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
                  // excel.Application.Workbooks.Add(true);//zj
                   wbs = excel.Workbooks;
                   wb = wbs[1];
                   ws = (Excel.Worksheet)wb.Worksheets["Sheet1"];
                   int rowCount = ws.UsedRange.Rows.Count;
                   int colCount = ws.UsedRange.Columns.Count;
                   //object[,] row = new object[rowCount-1, colCount];
                   for (int i = 0; i < rowCount-1; i++)//
                   {
                       DataRow mx;
                       mx = dsDB.Tables["DBMX"].NewRow();
                       object[] row = new object[colCount];//object[] row = new object[4];
                       for (int j = 0; j < colCount; j++)
                       {
                           range1 = ws.get_Range(ws.Cells[i + 2, j + 1],
                           ws.Cells[i + 2, j + 1]);
                           row[j] = range1.Value;
                       }
                           mx["LOT"] = lot;
                           mx["DBMX_DH"] = "D0101040101013";
                           mx["DBMX_GLM"] = row[0];//j
                           mx["DBMX_CC"] = row[1];
                           mx["DBMX_SL"] = row[2];
                           mx["DBMX_DW"] = "";
                           mx["STATE"] = 0;
                           mx["ERROR_FLAG"] = false;
                           mx["ERROR_REASON"] = "test";
                           mx["CREATED_BY"] = userID;
                           mx["CREATION_DATE"] = mytime;
                           mx["LAST_UPDATE_BY"] = userID;
                           mx["LAST_UPDATE_DATE"] = mytime;
                           dsDB.Tables["DBMX"].Rows.Add(mx);
                      

                   }
                   //dsDB.Tables["DBMX"].Rows.Add(mx);
                   POS.Update_DBMX_INTERFACE(BASEINFO.SerializationDataset(dsDB), lot.ToString(), 1); //??;@
               }
               finally
               {
                   if (excel != null)
                   {
                       if (wbs != null)
                       {
                           if (wb != null)
                           {
                               if (ws != null)
                               {
                                   if (range1 != null)
                                   {
                                       System.Runtime.InteropServices.Marshal.
                                       ReleaseComObject(range1);
                                       range1 = null;
                                   }
                                   System.Runtime.InteropServices.Marshal.
                                   ReleaseComObject(ws);
                                   ws = null;
                               }
                               wb.Close(false, Nothing, Nothing);
                               System.Runtime.InteropServices.Marshal.
                               ReleaseComObject(wb);
                               wb = null;
                           }
                           wbs.Close();
                           System.Runtime.InteropServices.Marshal.
                           ReleaseComObject(wbs);
                           wbs = null;
                       }
                       excel.Application.Workbooks.Close();
                       excel.Quit();
                       System.Runtime.InteropServices.Marshal.
                       ReleaseComObject(excel);
                       excel = null;
                       GC.Collect();
                   }
               }
             

               //catch (Exception E)
               //{
               //    if (sError != "")
               //        MessageBox.Show(sError);
               //    else
               //        MessageBox.Show(E.Message.ToString());
              //}
#endregion

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值