码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2020-01-28 17:12:58      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:process   font   reac   nbsp   use   collect   cell   mis   文件   

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(); 

                }

                

            }

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

标签:process   font   reac   nbsp   use   collect   cell   mis   文件   

原文地址:https://www.cnblogs.com/zrprj/p/12238310.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!