Asp.net读取Excel中合并单元格解决办法---成功(极品)

狂风中的少年 提交于 2020-01-28 17:05:04

Microsoft.Office.Interop.Excel.dll 版本是14.0

//文件上传

            btn_dr_fp_Click(null, null);

            string wjm = "";

            if (ViewState["zrpzy085"] != null)

            {

                wjm = ViewState["zrpzy085"].ToString();

            }

            System.Threading.Thread.Sleep(1000);

           

            string strFileName=Server.MapPath("~/exceldr/") + wjm;

            object missing = System.Reflection.Missing.Value;

            Application excel = new Application();//lauch excel application 

            if (excel == null)

            {

                Response.Write("<script>alert('Can't access excel')</script>");

            }

            else

            {

                excel.Visible = false; excel.UserControl = true;

                // 以只读的形式打开EXCEL文件 

                Workbook wb = excel.Application.Workbooks.Open(strFileName, missing, true, missing, missing, missing,

                 missing, missing, missing, true, missing, missing, missing, missing, missing);

                //取得第一个工作薄 

                Worksheet ws = (Worksheet)wb.Worksheets.get_Item(1);

                //取得总记录行数   (包括标题列) 

                int rowsint = ws.UsedRange.Cells.Rows.Count; //得到行数 

                //测试一下

               // this.Lbl_ts.Text = rowsint.ToString();打开excel成功

                //判断单元格是否合并

               // Range excelRange =ws.UsedRange;

                try

                {

 

                    /*

                    Range oCurCell;

                    oCurCell = ((Range)excelRange.Cells[6, 0]).MergeCells;

                    if(oCurCell!=null)

                    {

                        this.Lbl_ts.Text = "是合并单元格";

                    }

                     */

                   // Range subRange = (Range)excelRange.Cells[6, 0];

                    

                    Range rng1 = ws.Cells.get_Range("A8", "A8");

                    if(rng1.MergeCells)

                    {

                        this.Lbl_ts.Text = "是合并单元格";

                    }

                    else

                    {

                        this.Lbl_ts.Text = "不是合并单元格";

                    }

                }

                catch(Exception err)

                {

                    this.Lbl_ts.Text = err.Message;

                }

                

               finally

                {

                    excel.Quit(); excel = null;

                    Process[] procs = Process.GetProcessesByName("excel");

                    foreach (Process pro in procs)

                    {

                        pro.Kill();//没有更好的方法,只有杀掉进程 

                    }

                    GC.Collect(); 

                }

                

            }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!