COM读写EXCEL

/导出excel 

Microsoft.Office.Interop.Excel.ApplicationClass appC = new Microsoft.Office.Interop.Excel.ApplicationClass();
            Microsoft.Office.Interop.Excel.Workbook wkc = appC.Workbooks.Open(fileName, 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);
           // Microsoft.Office.Interop.Excel.Workbook wkc = appC.Workbooks.Add(Type.Missing);
            int ColRowIndex = 2;
            if (ultraGrid.DisplayLayout.Bands[0].Groups.Count <= 0)
            {
                ColRowIndex = 1;
            }
            if (wkc != null)
            {
                if (wkc.Worksheets.Count > 0)
                {
                    //wkc.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                    Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wkc.ActiveSheet;
                    ws.Cells.Clear();
                    //vSheet.OlePropertyGet("Rows", 3).OleProcedure("Insert");
                    try
                    {
                        int preCellIndex=1;
                        for (int r = 1; r <= ultraGrid.Rows.Count; r++)
                        {
                            for (int c = 1; c <= ultraGrid.DisplayLayout.Bands[0].Columns.Count; c++)
                            {
                               
                             
                                    if (r == 1 && c == 1)
                                    {//mysheet.get_Range((Excel.Range)mysheet.Cells[1,1],(Excel.Range)mysheet.Cells[1,2]).Merge
                                        //分组头信息
                                        for (int g = 1; g <= ultraGrid.DisplayLayout.Bands[0].Groups.Count; g++)
                                        {
                                            UltraGridGroup group = ultraGrid.DisplayLayout.Bands[0].Groups[g - 1];
                                            if (group.Columns.Count > 1)
                                            {
                                                ws.get_Range((Microsoft.Office.Interop.Excel.Range)ws.Cells[r, group.Columns[0].Index + 1],
                                                    (Microsoft.Office.Interop.Excel.Range)ws.Cells[r, group.Columns[0].Index + group.Columns.Count]).Merge(Type.Missing);
                                                preCellIndex += group.Columns.Count;
                                            }
                                            Microsoft.Office.Interop.Excel.Range rg = (Microsoft.Office.Interop.Excel.Range)ws.Cells[r, group.Columns[0].Index + 1];
                                            rg.Value2 = ultraGrid.DisplayLayout.Bands[0].Groups[g - 1].Header.Caption;
                                            rg.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                                            rg.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(204, 255, 204));
                                            rg.Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Gray);
                                        }
                                    }
                               
                                if (r == 1)
                                {//列名
                                    Microsoft.Office.Interop.Excel.Range rg0 = (Microsoft.Office.Interop.Excel.Range)ws.Cells[ColRowIndex, c];
                                    rg0.Value2 = ultraGrid.DisplayLayout.Bands[0].Columns[c - 1].Header.Caption;

                                    //Color backColor = ultraGrid.DisplayLayout.Bands[0].Columns[c - 1].Header.Appearance.BackColor;
                                    //if (backColor == Color.Black)
                                    //    backColor = Color.White;
                                    rg0.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(204, 255, 204));
                                    rg0.Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Gray);
                                }
                                Microsoft.Office.Interop.Excel.Range rg1 = (Microsoft.Office.Interop.Excel.Range)ws.Cells[r + ColRowIndex, c];
                                rg1.Value2 = ultraGrid.Rows[r - 1].Cells[c - 1].Text;
                                //rg1.ShrinkToFit = true;
                                Color backColor1 = ultraGrid.Rows[r - 1].Cells[c - 1].Appearance.BackColor;
                                //if(ultraGrid.Rows[r - 1].Cells[c - 1].Activated=Activation.no
                                if (backColor1 != Color.White && backColor1.Name!="0")
                                    rg1.Interior.Color = System.Drawing.ColorTranslator.ToOle(backColor1);
                                rg1.Borders.ColorIndex = 0;
                                rg1.Font.Color = System.Drawing.ColorTranslator.ToOle(ultraGrid.Rows[r - 1].Cells[c - 1].Appearance.ForeColor);
                            }
                        }
                        wkc.Save();
                            //As(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                           // Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                        //wkc.SaveCopyAs(fileName);
                        wkc.Close(true, fileName, false);
                    }
                    finally
                    {

                        if (appC != null)
                        {
                            appC.Quit();
                        }
                        if (wkc != null)
                        {
                            System.Runtime.InteropServices.Marshal.ReleaseComObject(wkc);
                            wkc = null;
                        }
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
                        ws = null;
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(appC);
                        appC = null;
                        GC.Collect();

                    }

                }
            } 

//

在导出的 excel中插入分组信息

//插入组信息
            if (ultraGrid.DisplayLayout.Bands.Count <= 0)
                return;
            if (ultraGrid.DisplayLayout.Bands[0].Groups.Count <= 0)
                return;
            Microsoft.Office.Interop.Excel.ApplicationClass appC = new Microsoft.Office.Interop.Excel.ApplicationClass();
            Microsoft.Office.Interop.Excel.Workbook wkc = appC.Workbooks.Open(fileName, 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);     
            if (wkc != null)
            {
                if (wkc.Worksheets.Count > 0)
                {
                    Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wkc.ActiveSheet;
                    try
                    {
                        Microsoft.Office.Interop.Excel.Range rg0 = (Microsoft.Office.Interop.Excel.Range)ws.Cells[1, 1];
                        //插入一空行
                        rg0.EntireRow.Insert(1, Type.Missing);                     
                                //分组头信息
                        int r = 1;//第一行
                        for (int g = 1; g <= ultraGrid.DisplayLayout.Bands[0].Groups.Count; g++)
                        {
                            UltraGridGroup group = ultraGrid.DisplayLayout.Bands[0].Groups[g - 1];
                            if (group.Columns.Count > 1)
                            {
                                ws.get_Range((Microsoft.Office.Interop.Excel.Range)ws.Cells[r, group.Columns[0].Index + 1],
                                    (Microsoft.Office.Interop.Excel.Range)ws.Cells[r, group.Columns[0].Index + group.Columns.Count]).Merge(Type.Missing);
                            }
                            Microsoft.Office.Interop.Excel.Range rg = (Microsoft.Office.Interop.Excel.Range)ws.Cells[r, group.Columns[0].Index + 1];
                            rg.Value2 = ultraGrid.DisplayLayout.Bands[0].Groups[g - 1].Header.Caption;
                            rg.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                            rg.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(204, 255, 204));
                            rg.Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Gray);
                        }                 
                        wkc.Save();                    
                        wkc.Close(true, fileName, false);
                    }                  
                    finally
                    {//kill excel processor
                        if (appC != null)
                        {
                            appC.Quit();
                        }
                        if (wkc != null)
                        {
                            System.Runtime.InteropServices.Marshal.ReleaseComObject(wkc);
                            wkc = null;
                        }
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
                        ws = null;
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(appC);
                        appC = null;
                        GC.Collect();
                    }
                }
            }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值