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

2021-04-23 15:29

阅读:355

标签: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("");

            }

            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


评论


亲,登录后才可以留言!