c# 读写excel文件

**首先项目要引用excel.dll:Microsoft.Office.Interop.Excel

  /// 
            public string[] Excel_Read(string filePath, string[] cellAddress,string workSheetName)
            {
             if (!File.Exists(fileFullName))
            {
                return null;    
            }
                List<string> listExcelVal = new List<string>();
                Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbooks wbooks = xlApp.Workbooks;
               // string fileCopyTemp = FileCopy(filePath); 
                Microsoft.Office.Interop.Excel.Workbook wbook = wbooks.Open(filePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                Microsoft.Office.Interop.Excel.Worksheet worksheet = wbook.Worksheets[workSheetName];   //"Inf" sheetname
               try
                {   
      // Write  the data
      //for (int j = 0; j <= cellAddress.Length - 1; j++)
         //           {
      //  worksheet.Range(cellAddress(i).ToString()).Value2 = "test test test";
      //  }
                    // read the data   ,support address range ,eg: [B8:B49]                        
                  for (int j = 0; j <= cellAddress.Length - 1; j++)
                {
                    switch (cellAddress[j].ToString())
                    {
                        case "":
                            listExcelVal.Add("");
                            break;                          
                        default:
                            object obj;                           
                               Microsoft .Office .Interop.Excel .Range range = worksheet.Range[cellAddress[j].ToString()];
                                foreach (Microsoft.Office.Interop.Excel.Range cell in range)
                                {
                                    obj = cell.Text;
                                    listExcelVal.Add(obj.ToString());
                                }
                                //obj = worksheet.Range[cellAddress[j].ToString()].Value2;     // obj is a Object[,] value
                                //foreach (var item in (object[,])obj)
                                //{
                                //    // foreach (var item in  (object[])items)    
                                //    //  {
                                //    listExcelVal.Add(Convert.ToString(item));
                                //    listExcelVal.Add(item.GetType().Name.ToUpper() == "DOUBLE" ? (Convert.ToDouble(Convert.ToString(item)).ToString("f2")) : Convert.ToString(item));
                                //    //}
                                //}                                               
                            break;
                    }                  
                  }
                }
                catch (Exception e)
                {
                // throw e;
                  listExcelVal.Clear();
                  listExcelVal.Add("Error in getting excel data.");
                }
                finally
                {
                    wbook.Close(false, filePath, false); //不會彈出"是否保存"框
                    xlApp.Quit();
                    ReleaseObj(xlApp);
                    ReleaseObj(wbook);
                    ReleaseObj(worksheet);                                    
                }
                 return listExcelVal.ToArray();
        }

        //释放资源
        private void ReleaseObj(Object o)
        {
            try
            {
                //使用此方法,来释放引用某些资源的基础 COM 对象。 这里的o就是要释放的对象
                System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
            }
            catch { }
            finally
            {
                o = null; GC.Collect();
            }
        }

你可能感兴趣的:(基础知识,Winform)