string m_ApplicationPath = Request.ApplicationPath;
Microsoft.Office.Interop.Excel.Application myExcel=new Microsoft.Office.Interop.Excel.Application ( );myExcel.Application.Workbooks.Open(m_newPath, missing, missing, missing, missing,
            missing,missing,missing,missing,missing,missing, missing,missing,missing,missing); myExcel.Visible = true;myExcel.Cells[1, 1] = "2";Microsoft.Office.Interop.Excel.Workbook myBook = myExcel.Workbooks[1];myBook.Save();
在网上找的一断代码,通过EXCEL模板,复制得到一个新的EXCEL,对这个新的EXCEL进行操作。
比如对1行1列赋值为2.
现在有个问题,就是加入我想将1行2列,和2行2列,进行合并,然后赋值,
该如何操作呢?

解决方案 »

  1.   

    给个例子你看: SqlConnection conn = new SqlConnection(connstr);
            SqlDataAdapter sda = new SqlDataAdapter(sql1.Text, conn);        DataSet ds = new DataSet();
            conn.Open();
            sda.Fill(ds);
            conn.Close();        OWC10.SpreadsheetClass xlsheet;
            xlsheet= new OWC10.SpreadsheetClass();
            DataRow dr;
            int i = 0;
            for(int ii=0;ii<ds.Tables[0].Rows.Count;ii++)
            {
                dr = ds.Tables[0].Rows[ii];
                
                 
                
                //合并单元格
                xlsheet.get_Range(xlsheet.Cells[i+1, 1], xlsheet.Cells[i+1, 8]).set_MergeCells(true);
                
                xlsheet.get_Range(xlsheet.Cells[i + 5, 1], xlsheet.Cells[i + 5, 3]).set_MergeCells(true);
                xlsheet.get_Range(xlsheet.Cells[i + 5, 4], xlsheet.Cells[i + 5, 6]).set_MergeCells(true);
                xlsheet.get_Range(xlsheet.Cells[i + 5, 7], xlsheet.Cells[i + 5, 8]).set_MergeCells(true);            xlsheet.ActiveSheet.Cells[i + 1, 1] = dr["姓名"].ToString() + "自然情况";
                //字体加粗
                xlsheet.get_Range(xlsheet.Cells[i + 1, 1], xlsheet.Cells[i + 1, 14]).Font.set_Bold(true);
                //单元格文本水平居中对齐
                xlsheet.get_Range(xlsheet.Cells[i + 1, 1], xlsheet.Cells[i + 1, 14]).set_HorizontalAlignment(OWC10.XlHAlign.xlHAlignCenter);
                //设置字体大小
                xlsheet.get_Range(xlsheet.Cells[i + 1, 1], xlsheet.Cells[i + 1, 14]).Font.set_Size(14);
                //设置列宽
                xlsheet.get_Range(xlsheet.Cells[i + 1, 8], xlsheet.Cells[i + 1, 8]).set_ColumnWidth(20);
                
                //画边框线
                xlsheet.get_Range(xlsheet.Cells[i + 1, 1], xlsheet.Cells[i+5, 8]).Borders.set_LineStyle(OWC10.XlLineStyle.xlContinuous);
                //写入数据  (这里由DS生成)            xlsheet.ActiveSheet.Cells[i + 2, 1] = "姓名";
                xlsheet.ActiveSheet.Cells[i + 2, 2] = dr["姓名"].ToString();
                xlsheet.ActiveSheet.Cells[i + 2, 3] = "曾用名";
                xlsheet.ActiveSheet.Cells[i + 2, 4] = dr["曾用名"].ToString();
                xlsheet.ActiveSheet.Cells[i + 2, 5] = "出生年月";
                xlsheet.ActiveSheet.Cells[i + 2, 6] = DateTime.Parse(dr["出生年月"].ToString()).Year.ToString() + "-" + DateTime.Parse(dr["出生年月"].ToString()).Month.ToString();
                xlsheet.ActiveSheet.Cells[i + 2, 7] = " 参加工作时间";
                xlsheet.ActiveSheet.Cells[i + 2, 8] = DateTime.Parse(dr["参加工作时间"].ToString()).Year.ToString() + "-" + DateTime.Parse(dr["参加工作时间"].ToString()).Month.ToString();            xlsheet.ActiveSheet.Cells[i + 3, 1] = "性别";
                xlsheet.ActiveSheet.Cells[i + 3, 2] = dr["性别"].ToString();
                xlsheet.ActiveSheet.Cells[i + 3, 3] = "民族";
                xlsheet.ActiveSheet.Cells[i + 3, 4] = dr["民族"].ToString();
                xlsheet.ActiveSheet.Cells[i + 3, 5] = "政治面貌";
                xlsheet.ActiveSheet.Cells[i + 3, 6] = dr["政治面貌"].ToString();
                xlsheet.ActiveSheet.Cells[i + 3, 7] = "职称";
                xlsheet.ActiveSheet.Cells[i + 3, 8] = dr["职称"].ToString();            xlsheet.ActiveSheet.Cells[i + 4, 1] = "学历";
                xlsheet.ActiveSheet.Cells[i + 4, 2] = dr["学历"].ToString();
                xlsheet.ActiveSheet.Cells[i + 4, 3] = "学位";
                xlsheet.ActiveSheet.Cells[i + 4, 4] = dr["学位"].ToString();
                xlsheet.ActiveSheet.Cells[i + 4, 5] = "职务";
                xlsheet.ActiveSheet.Cells[i + 4, 6] = dr["职务"].ToString();
                xlsheet.ActiveSheet.Cells[i + 4, 7] = "档案号码";
                //Excel不支持0开头输入,加上姓氏首字母正好是编号全称
                xlsheet.ActiveSheet.Cells[i + 4, 8] = dr["姓氏首字母"].ToString() + dr["档案号码"].ToString();
                xlsheet.ActiveSheet.Cells[i + 5, 1] = "现从事专业:" + dr["现从事专业"].ToString();
                xlsheet.ActiveSheet.Cells[i + 5, 4] = "工作单位:" + dr["工作单位"].ToString();
                xlsheet.ActiveSheet.Cells[i + 5, 7] = "身份证:" + dr["身份证号"].ToString();            i += 6;
            }
            try
            {
                string D = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
                DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString()+
                DateTime.Now.Millisecond.ToString();            xlsheet.Export(Server.MapPath("./")+"\\"+D+".xls", OWC10.SheetExportActionEnum.ssExportActionNone, OWC10.SheetExportFormat.ssExportXMLSpreadsheet);
                Response.Write("<script>window.open('"+D+".xls')</script>");
            }
            catch
            {
            }
        }
      

  2.   

     string m_ApplicationPath = Request.ApplicationPath;
                Microsoft.Office.Interop.Excel.Application myExcel = new Microsoft.Office.Interop.Excel.Application();
                object missing = Type.Missing;
                string m_newPath = "d:\\a.xls";
                Microsoft.Office.Interop.Excel.Workbook myBook = (Microsoft.Office.Interop.Excel.Workbook)myExcel.Application.Workbooks.Open(m_newPath, missing, missing, missing, missing,
                            missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);          
             
                myExcel.Visible = false;            myExcel.Cells[1, 1] = "2";
                Microsoft.Office.Interop.Excel.Range cell1 = (Microsoft.Office.Interop.Excel.Range)myExcel.Cells[1, 2];
                Microsoft.Office.Interop.Excel.Range cell2 = (Microsoft.Office.Interop.Excel.Range)myExcel.Cells[2, 2];
                Microsoft.Office.Interop.Excel.Range c = myExcel.get_Range(cell1, cell2);
                c.Merge(false);
                c.FormulaR1C1 = "新的值";
                myBook.Save();
                myBook.Close(missing, missing, missing);
                cell1 = null;
                cell2 = null;
                c = null;
                myBook = null;
                myExcel.Quit();
                myExcel = null;
      

  3.   

    首先很感谢上面的几位朋友。这个问题我基本解决了,但是又有个新的问题
                //合并单元格
                Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcel.ActiveSheet;            Microsoft.Office.Interop.Excel.Range rH = worksheet.get_Range("A20", "A23");
                rH.Merge(0);
                rH.Font.Bold = true;//粗体
                rH.Font.Size = 10;
                rH.Cells.HorizontalAlignment = ???;
                //
    我现在可以设置字体加粗,字体大小,但是我想让水平居中,该如何设置呢?
      

  4.   

     Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;