應用c#從Excel讀取資料

來源:互聯網
上載者:User

 //初始化對象

Application app = null;

            Workbook MyBook = null;

            Worksheet mySheet = null;

            string sPath = ConfigurationManager.AppSettings["ExportTemp"].ToString();
            string sMsg = "";
            try
            {
                sPath += Path.GetFileName(file.FileName);

                try
                {
                    file.SaveAs(sPath);

                    ModelSurveyCommon oModel = new ModelSurveyCommon();

//建立對象

                    app = new Application();
                    MyBook = app.Workbooks.Open(sPath, 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);
                    mySheet = (Worksheet)MyBook.Worksheets[1];
                    Range cellB;
                    string sValue, aTemp = "";
                    cellB = (Range)mySheet.Cells[1, 1];//取得儲存格[1,1]的資料

                    if (cellB.Value2.ToString() == "SURVEY_COMMON")
                    {
                        for (int j = 1; j < mySheet.UsedRange.Columns.Count + 1; j++)
                        {
                            cellB = (Range)mySheet.Cells[2, j];
                            if (cellB.Value2 != "" && cellB.Value2 != null)
                            {
                                aTemp = aTemp + cellB.Value2.ToString() + "#^$@@";
                            }
                            else
                            {
                                aTemp = aTemp + "#^$@@";
                            }
                        }
                        string[] sFieldName = Common.Common.SplitString(aTemp, "#^$@@");
                        //Response.Write("aTemp:" + aTemp + "<br>");
                        for (int i = 3; i < mySheet.UsedRange.Rows.Count + 1; i++)
                        {
                            sValue = "";
                            for (int j = 1; j < mySheet.UsedRange.Columns.Count + 1; j++)
                            {
                                cellB = (Range)mySheet.Cells[i, j];
                                if (cellB.Value2 != "" && cellB.Value2 != null)
                                {
                                    sValue = sValue + cellB.Value2.ToString() + "#^$@@";
                                }
                                else
                                {
                                    sValue = sValue + "#^$@@";
                                }
                            }
                            //Response.Write("sValue:" + sValue);
                            //Response.Write("sValue:" + sValue + "<br>");
                            string[] sFiledValue = Common.Common.SplitString(sValue, "#^$@@");

                            oModel.ImportSurvey(sFieldName, sFiledValue);
                        }
                        sMsg = "Import Successfully.";
                    }
                    else
                    {
                        sMsg = "Table incorrect.";
                    }

                }
                catch
                {
                    sMsg = "Import data error.";
                }
            }
            catch
            {
                sMsg = "Please choose file.";
            }

 

//清Null 物件

            if (mySheet != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(mySheet);
                mySheet = null;
            }
            if (MyBook != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(MyBook);
                MyBook = null;
            }

            //關閉對象
            app.Workbooks.Close();
            app.Quit();

            if (app != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
                app = null;
            }
            GC.Collect(); //記憶體回收

 

 

 

 

利用二組數組匯入資料到EXCEL

 string sPath =@"C:/Inetpub/wwwroot/test_20110222/test/Report/";
            string sFileName = "test_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
            sPath += sFileName;

            System.IO.File.Copy(@"C:/Inetpub/wwwroot/test_20110222/test/Report/test.xls", sPath, true);

            Application app = null;
            Workbook MyBook = null;
            Worksheet mySheet = null;
            app = new Application();
            MyBook = app.Workbooks.Open(sPath, 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);
            mySheet = (Worksheet)MyBook.Worksheets[1];
            int row = 10000, colCount =30;
            string[,] ss = new string[row, colCount];

            for (int j = 1; j < row; j++)
            {
                for (int k = 1; k < colCount; k++)
                {
                    ss[j, k] = j+":"+k;
                   // mySheet.Cells[j, k] = j + ":" + k;
                }
            }  

            mySheet.get_Range("A2", "Z10000").Value2 = ss;
           
            MyBook.Save();

            if (mySheet != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(mySheet);
                mySheet = null;
            }
            if (MyBook != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(MyBook);
                MyBook = null;
            }
            //關閉對象
            app.Workbooks.Close();
            app.Quit();

            if (app != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
                app = null;
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.