Excel操作代码

using System;
using System.Collections.Generic;
using System.Text;
using Excel;
using System.Reflection;
using GOISPC.Info;
using GOISPC.Manager;
using System.Drawing;
using System.IO;
using Microsoft.Office.Core;
using Languages;
using System.Data.OleDb;


namespace SPC
{

    public class CExport
    {
        private IntPtr recHandle;


        public IntPtr RecHandle
        {
            get { return recHandle; }
            set { recHandle = value; }
        }
        private List<CTest_SubBoard> boardList;


        public List<CTest_SubBoard> BoardList
        {
            get { return boardList; }
            set { boardList = value; }
        }
        private List<CTest_Comp_NG> combList;


        public List<CTest_Comp_NG> CombList
        {
            get { return combList; }
            set { combList = value; }
        }
        private List<CTest_Tw_NG> winList;


        public List<CTest_Tw_NG> WinList
        {
            get { return winList; }
            set { winList = value; }
        }
        private CBoardAndCombRate eleRate;


        public CBoardAndCombRate EleRate
        {
            get { return eleRate; }
            set { eleRate = value; }
        }
        private List<CCombFtRate> combRateList;


        public List<CCombFtRate> CombRateList
        {
            get { return combRateList; }
            set { combRateList = value; }
        }
        private List<CWinRate> winRateList;


        public List<CWinRate> WinRateList
        {
            get { return winRateList; }
            set { winRateList = value; }
        }
        private List<CWinCodeRate> codeRateList;


        public List<CWinCodeRate> CodeRateList
        {
            get { return codeRateList; }
            set { codeRateList = value; }
        }


        private List<CBoardRate> _bdRateList;


        public List<CBoardRate> BdRateList
        {
            get { return _bdRateList; }
            set { _bdRateList = value; }
        }


        private DateTime beginTime;


        public DateTime BeginTime
        {
            get { return beginTime; }
            set { beginTime = value; }
        }
        private DateTime endTime;


        public DateTime EndTime
        {
            get { return endTime; }
            set { endTime = value; }
        }
        private string pcbfileName;


        public string PcbfileName
        {
            get { return pcbfileName; }
            set { pcbfileName = value; }
        }
        private string proName;


        public string ProName
        {
            get { return proName; }
            set { proName = value; }
        }


        private string saveFileName;


        public string SaveFileName
        {
            get { return saveFileName; }
            set { saveFileName = value; }
        }


        public CExport()
        {
            boardList = null;
            combList = null;
            winList = null;
            eleRate = null;
            combRateList = null;
            winRateList = null;
            codeRateList = null;
            beginTime = DateTime.Now;
            endTime = DateTime.Now;
            pcbfileName = "";
            proName = "";
            saveFileName = "";
            recHandle = IntPtr.Zero;
            _bdRateList = null;
        }


        public void Export()
        {
            MultiLanguage.SetLanguage(ConfigManager.GetInstance().Language);
            CExcelManager excelManager = null;
            try
            {
                excelManager = new CExcelManager();
                excelManager.Create();
                int count = 0;
                if (boardList != null)
                {
                    string sheetName = MultiLanguage.GetString("SPC_Record");
                    if (sheetName == "")
                    {
                        sheetName = "sheet" + count;
                        count++;
                    }
                    excelManager.AddWorkSheet(sheetName);
                    excelManager.WriteBoardList(boardList, pcbfileName, proName, beginTime, endTime);
                }
                if (winList != null)
                {
                    string sheetName = MultiLanguage.GetString("SPC_W_NGDetail");
                    if (sheetName == "")
                    {
                        sheetName = "sheet" + count;
                        count++;
                    }
                    excelManager.AddWorkSheet(sheetName);
                    excelManager.WriteWinList(winList, pcbfileName, proName, beginTime, endTime);
                }
                if (combList != null)
                {
                    string sheetName = MultiLanguage.GetString("SPC_E_NGDetail");
                    if (sheetName == "")
                    {
                        sheetName = "sheet" + count;
                        count++;
                    }
                    excelManager.AddWorkSheet(sheetName);
                    excelManager.WriteCombList(combList, pcbfileName, proName, beginTime, endTime);
                }
                if (eleRate != null)
                {
                    string sheetName = MultiLanguage.GetString("SPC_BoardRate");
                    if (sheetName == "")
                    {
                        sheetName = "sheet" + count;
                        count++;
                    }
                    excelManager.AddWorkSheet(sheetName);
                    excelManager.WriteBoardAndCombRate(eleRate, null, pcbfileName, proName, beginTime, endTime);
                }
                if (combRateList != null)
                {
                    string sheetName = MultiLanguage.GetString("SPC_ELEPassrate");
                    if (sheetName == "")
                    {
                        sheetName = "sheet" + count;
                        count++;
                    }
                    excelManager.AddWorkSheet(sheetName);
                    excelManager.WriteCombRate(combRateList, null, pcbfileName, proName, beginTime, endTime);
                }
                if (codeRateList != null)
                {
                    string sheetName = MultiLanguage.GetString("SPC_NGType");
                    if (sheetName == "")
                    {
                        sheetName = "sheet" + count;
                        count++;
                    }
                    excelManager.AddWorkSheet(sheetName);
                    excelManager.WriteWinCodeRate(codeRateList, null, pcbfileName, proName, beginTime, endTime);
                }


                if (BdRateList != null)
                {
                    string sheetName = MultiLanguage.GetString("SPC_BoardRate");
                    if (sheetName == "")
                    {
                        sheetName = "sheet" + count;
                        count++;
                    }
                    excelManager.AddWorkSheet(sheetName);
                    excelManager.WriteBoardRate(BdRateList, beginTime, endTime);
                }
                excelManager.Save(saveFileName);
                MsgLib.SendMessage((int)recHandle, MsgLib.MSG_EXPORT_FINISH, 0, 0);
            }
            catch (Exception ex)
            {
                SPCLog.GetInstance().WriteSYS(ex.ToString());
                MsgLib.SendMessage((int)recHandle, MsgLib.MSG_EXPORT_ERROR, 0, 0);
            }
            finally
            {
                if (excelManager != null)
                {
                    excelManager.Close();
                }
            }
            ConfigManager.GetInstance().SPCState = ConfigManager.EStatue.EXPORTFINISH;
        }

    }



    public class CExcelManager
    {
        private Application app;
        private Workbooks wbks;
        private Workbook wbk;
        private ExcelCell currSheet;
        private Excel.Worksheet activeSheet;


        private string _fileName;




        public CExcelManager()
        {
            app = new Application();
            wbks = app.Workbooks;


            wbk = null;
            currSheet = null;
            _fileName = "";
        }


        public void Open(string fileName)
        {
            wbk = wbks.Add(fileName);
            _fileName = fileName;
        }


        public void Create()
        {
            wbk = wbks.Add(true);
        }


        public ExcelCell AddWorkSheet(string name)
        {
            Excel.Worksheet sheet = (Excel.Worksheet)wbk.Worksheets.Add(Type.Missing, Type.Missing, 1, Type.Missing);
            activeSheet = sheet;
            sheet.Name = name;
            currSheet = new ExcelCell(sheet);
            return currSheet;
        }


        public void WriteData(string data, int row, int cell)
        {
            if (currSheet != null)
            {
                currSheet[row, cell] = data;
            }
        }


        public void WriteBoardList(List<CTest_SubBoard> boardList, string fileName, string proName, DateTime beginTime, DateTime endTime)
        {
            object[,] arrTitle = new object[4, 2];
            arrTitle[0, 0] = MultiLanguage.GetString("SPC_FileName");
            arrTitle[0, 1] = fileName;
            arrTitle[1, 0] = proName;
            arrTitle[1, 1] = proName;
            arrTitle[2, 0] = MultiLanguage.GetString("SPC_BeginTime");
            arrTitle[2, 1] = DefineConst.FormatDateYYYYMMDD_HHMMSS(beginTime);
            arrTitle[3, 0] = MultiLanguage.GetString("SPC_EndTime");
            arrTitle[3, 1] = DefineConst.FormatDateYYYYMMDD_HHMMSS(endTime);
            currSheet[1, 4, 2] = arrTitle;


            int startRow = 7;
            CellsDrawFrame(startRow, 1, startRow + boardList.Count, 10);
            currSheet.SetColWidth(startRow, 20);


            object[,] arr = new object[boardList.Count + 1, 10];
            int i = 0;
            arr[i, 0] = MultiLanguage.GetString("SPC_TESTID");
            arr[i, 1] = MultiLanguage.GetString("SPC_FileName");
            arr[i, 2] = MultiLanguage.GetString("SPC_bar");
            arr[i, 3] = MultiLanguage.GetString("SPC_ELENum");
            arr[i, 4] = MultiLanguage.GetString("SPC_AlarmNum");
            arr[i, 5] = MultiLanguage.GetString("SPC_MisNum");
            arr[i, 6] = MultiLanguage.GetString("SPC_NgNum");
            arr[i, 7] = MultiLanguage.GetString("SPC_Result");
            arr[i, 8] = MultiLanguage.GetString("SPC_Operator");
            arr[i, 9] = MultiLanguage.GetString("SPC_TestTime");
            for (i = 0; i < boardList.Count; i++)
            {
                if (ConfigManager.GetInstance().SPCState == ConfigManager.EStatue.EXIT || ConfigManager.GetInstance().SPCState == ConfigManager.EStatue.STOPEXPORT)
                {
                    break;
                }


                CTest_SubBoard info = boardList[i];
                int index = i + 1;
                arr[index, 0] = info.Test_ID;
                arr[index, 1] = info.CBoard.BoardName;
                arr[index, 2] = info.Barcode;
                arr[index, 3] = info.TotalEleNum;
                arr[index, 4] = info.SrcErrNum;
                arr[index, 5] = info.ErrNum;
                arr[index, 6] = info.DestErrNum;
                if (info.DsResult > 0)
                {
                    arr[index, 7] = "fail";
                }
                else
                {
                    arr[index, 7] = "pass";
                }
                arr[index, 8] = info.CUser.Name;
                arr[index, 9] = DefineConst.FormatDateYYYYMMDD_HHMMSS(info.Test_Date);
            }
            currSheet[startRow, boardList.Count + 1, 10] = arr;
        }

        public void WriteCombRate(List<CCombFtRate> combRateList, Bitmap bmp, string fileName, string proName, DateTime beginTime, DateTime endTime)
        {
            object[,] arrTitle = new object[4, 2];
            arrTitle[0, 0] = MultiLanguage.GetString("SPC_FileName");
            arrTitle[0, 1] = fileName;
            arrTitle[1, 0] = "";
            arrTitle[1, 1] = "";
            arrTitle[2, 0] = MultiLanguage.GetString("SPC_BeginTime");
            arrTitle[2, 1] = DefineConst.FormatDateYYYYMMDD_HHMMSS(beginTime);
            arrTitle[3, 0] = MultiLanguage.GetString("SPC_EndTime");
            arrTitle[3, 1] = DefineConst.FormatDateYYYYMMDD_HHMMSS(endTime);
            currSheet[1, 4, 2] = arrTitle;


            int startRow = 7;
            CellsDrawFrame(startRow, 1, startRow + combRateList.Count, 3);
            currSheet.SetColWidth(startRow, 20);


            object[,] arr = new object[combRateList.Count + 1, 3];
            arr[0, 0] = MultiLanguage.GetString("SPC_ELEName");
            arr[0, 1] = MultiLanguage.GetString("SPC_TotalNum");
            arr[0, 2] = MultiLanguage.GetString("SPC_NgNum");


            int index = 1;
            foreach (CCombFtRate info in combRateList)
            {
                arr[index, 0] = info.CombName;
                arr[index, 1] = info.CombNum;
                arr[index, 2] = info.ManualErrNum;
                index++;
            }
            currSheet[startRow, combRateList.Count + 1, 3] = arr;
            CreateChart(Excel.XlChartType.xlColumnClustered, Excel.XlRowCol.xlColumns, new System.Drawing.Point(7, 1), new System.Drawing.Point(7 + combRateList.Count, 3));
        }


        public void WriteWinRate(List<CWinRate> winRateList, Bitmap bmp, string fileName, string proName, DateTime beginTime, DateTime endTime)
        {
            object[,] arrTitle = new object[4, 2];
            arrTitle[0, 0] = MultiLanguage.GetString("SPC_FileName");
            arrTitle[0, 1] = fileName;
            arrTitle[1, 0] = "";
            arrTitle[1, 1] = "";
            arrTitle[2, 0] = MultiLanguage.GetString("SPC_BeginTime");
            arrTitle[2, 1] = DefineConst.FormatDateYYYYMMDD_HHMMSS(beginTime);
            arrTitle[3, 0] = MultiLanguage.GetString("SPC_EndTime");
            arrTitle[3, 1] = DefineConst.FormatDateYYYYMMDD_HHMMSS(endTime);
            currSheet[1, 4, 2] = arrTitle;




            int startRow = 7;
            CellsDrawFrame(startRow, 1, startRow + winRateList.Count, 3);
            currSheet.SetColWidth(startRow, 20);


            object[,] arr = new object[winRateList.Count + 1, 3];
            arr[0, 0] = MultiLanguage.GetString("SPC_WinName");
            arr[0, 1] = MultiLanguage.GetString("SPC_AlarmNum");
            arr[0, 2] = MultiLanguage.GetString("SPC_NgNum");
            int index = 1;
            foreach (CWinRate info in winRateList)
            {
                arr[index, 0] = info.WinName;
                arr[index, 1] = info.MechineErrNum;
                arr[index, 2] = info.ManualErrNum;
                index++;
            }
            currSheet[startRow, winRateList.Count + 1, 3] = arr;
            CreateChart(Excel.XlChartType.xlColumnClustered, Excel.XlRowCol.xlColumns, new System.Drawing.Point(7, 1), new System.Drawing.Point(7 + winRateList.Count, 3));
        }


        public void Save()
        {
            if (_fileName != "")
            {
                Save(_fileName);
            }
        }


        public void Save(string fileName)
        {
            _fileName = fileName;
            wbk.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        }


        public void SaveAs(string newFileName)
        {
            _fileName = newFileName;
            wbk.SaveAs(newFileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        }


        public bool Close()
        {
            try
            {
                if (_fileName != "")
                {
                    wbk.Close(true, _fileName, Type.Missing);


                }
                else
                {
                    wbk.Close(Type.Missing, Type.Missing, Type.Missing);
                }
                wbks.Close();
                app.Quit();
            }
            finally
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(wbk);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(wbks);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(app);


                wbk = null;
                wbks = null;
                app = null;
            }
            return true;
        }


        /// <summary>
        /// 绘制指定单元格的边框
        /// </summary>
        /// <param name="startRow">起始行</param>
        /// <param name="startColumn">起始列</param>
        /// <param name="endRow">结束行</param>
        /// <param name="endColumn">结束列</param>
        public void CellsDrawFrame(int startRow, int startColumn, int endRow, int endColumn)
        {
            if (startRow != endRow)
            {
                CellsDrawFrame(startRow, startColumn, endRow, endColumn,
                    true, true, true, true, true, true, false, false,
                    LineStyle.A, BorderWeight.B, ColorIndex.B);
            }
        }


        public void CellsDrawFrame(int startRow, int startColumn, int endRow, int endColumn,
    bool isDrawTop, bool isDrawBottom, bool isDrawLeft, bool isDrawRight,
    bool isDrawHInside, bool isDrawVInside, bool isDrawDiagonalDown, bool isDrawDiagonalUp,
    LineStyle lineStyle, BorderWeight borderWeight, ColorIndex color)
        {
            //获取画边框的单元格  必须单独写出,否则0ffice 2007 不识别
            object cel1 = app.Cells[startRow, startColumn];
            object cel2 = app.Cells[endRow, endColumn];
            Excel.Range range = app.get_Range(cel1, cel2);
            // Excel.Range range = app.get_Range(app.Cells[startRow, startColumn], app.Cells[endRow, endColumn]);//这样写在office2007异常死机。


            //清除所有边框
            range.Borders[XlBordersIndex.xlEdgeTop].LineStyle = LineStyle.G;
            range.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = LineStyle.G;
            range.Borders[XlBordersIndex.xlEdgeLeft].LineStyle = LineStyle.G;
            range.Borders[XlBordersIndex.xlEdgeRight].LineStyle = LineStyle.G;
            range.Borders[XlBordersIndex.xlInsideHorizontal].LineStyle = LineStyle.G;
            range.Borders[XlBordersIndex.xlInsideVertical].LineStyle = LineStyle.G;
            range.Borders[XlBordersIndex.xlDiagonalDown].LineStyle = LineStyle.G;
            range.Borders[XlBordersIndex.xlDiagonalUp].LineStyle = LineStyle.G;


            //以下是按参数画边框 
            if (isDrawTop)
            {
                range.Borders[XlBordersIndex.xlEdgeTop].LineStyle = lineStyle;
                range.Borders[XlBordersIndex.xlEdgeTop].Weight = borderWeight;
                range.Borders[XlBordersIndex.xlEdgeTop].ColorIndex = color;
            }


            if (isDrawBottom)
            {
                range.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = lineStyle;
                range.Borders[XlBordersIndex.xlEdgeBottom].Weight = borderWeight;
                range.Borders[XlBordersIndex.xlEdgeBottom].ColorIndex = color;
            }


            if (isDrawLeft)
            {
                range.Borders[XlBordersIndex.xlEdgeLeft].LineStyle = lineStyle;
                range.Borders[XlBordersIndex.xlEdgeLeft].Weight = borderWeight;
                range.Borders[XlBordersIndex.xlEdgeLeft].ColorIndex = color;
            }


            if (isDrawRight)
            {
                range.Borders[XlBordersIndex.xlEdgeRight].LineStyle = lineStyle;
                range.Borders[XlBordersIndex.xlEdgeRight].Weight = borderWeight;
                range.Borders[XlBordersIndex.xlEdgeRight].ColorIndex = color;
            }


            if (isDrawVInside)
            {
                range.Borders[XlBordersIndex.xlInsideVertical].LineStyle = lineStyle;
                range.Borders[XlBordersIndex.xlInsideVertical].Weight = borderWeight;
                range.Borders[XlBordersIndex.xlInsideVertical].ColorIndex = color;
            }


            if (isDrawHInside)
            {
                range.Borders[XlBordersIndex.xlInsideHorizontal].LineStyle = lineStyle;
                range.Borders[XlBordersIndex.xlInsideHorizontal].Weight = borderWeight;
                range.Borders[XlBordersIndex.xlInsideHorizontal].ColorIndex = color;
            }


            if (isDrawDiagonalDown)
            {
                range.Borders[XlBordersIndex.xlDiagonalDown].LineStyle = lineStyle;
                range.Borders[XlBordersIndex.xlDiagonalDown].Weight = borderWeight;
                range.Borders[XlBordersIndex.xlDiagonalDown].ColorIndex = color;
            }


            if (isDrawDiagonalUp)
            {
                range.Borders[XlBordersIndex.xlDiagonalUp].LineStyle = lineStyle;
                range.Borders[XlBordersIndex.xlDiagonalUp].Weight = borderWeight;
                range.Borders[XlBordersIndex.xlDiagonalUp].ColorIndex = color;
            }
        }


        public void CreateChart(Excel.XlChartType ChartType, Excel.XlRowCol XlRowCol, System.Drawing.Point LT, System.Drawing.Point RB)
        {
            try
            {
                Excel.Worksheet worksheet = (Excel.Worksheet)activeSheet; //(Excel.Worksheet)app.ActiveSheet;
                Excel.Range oResizeRange;


                wbk.Charts.Add(Missing.Value, Missing.Value, 1, Missing.Value);
                wbk.ActiveChart.ChartType = ChartType;//设置图形


                //设置数据取值范围 必须单独写出,否则0ffice 2007 不识别
                object cel1 = worksheet.Cells[LT.X, LT.Y];
                object cel2 = worksheet.Cells[RB.X, RB.Y];
                wbk.ActiveChart.SetSourceData(worksheet.get_Range(cel1, cel2), XlRowCol);
                //m_Book.ActiveChart.Location(Excel.XlChartLocation.xlLocationAutomatic, title);
                //以下是给图表放在指定位置
                wbk.ActiveChart.Location(Excel.XlChartLocation.xlLocationAsObject, worksheet.Name);
                oResizeRange = (Excel.Range)worksheet.Rows.get_Item(10, Missing.Value);
                object index = 0; //office 2007 或者 wps
                try
                {
                    worksheet.Shapes.Item(index).Top = (float)(double)oResizeRange.Top;  //调图表的位置上边距
                }
                catch (System.Exception ex)
                {
                    index = "Chart 1"; //office 2003 使用
                    worksheet.Shapes.Item(index).Top = (float)(double)oResizeRange.Top;  //调图表的位置上边距
                }


                oResizeRange = (Excel.Range)worksheet.Columns.get_Item(6, Missing.Value);  //调图表的位置左边距


                worksheet.Shapes.Item(index).Width = 400;   //调图表的宽度
                worksheet.Shapes.Item(index).Height = 250;  //调图表的高度


                wbk.ActiveChart.PlotArea.Interior.ColorIndex = 19;  //设置绘图区的背景色 
                wbk.ActiveChart.PlotArea.Border.LineStyle = Excel.XlLineStyle.xlLineStyleNone;//设置绘图区边框线条
                wbk.ActiveChart.PlotArea.Width = 400;   //设置绘图区宽度
                //m_Book.ActiveChart.ChartArea.Interior.ColorIndex = 10; //设置整个图表的背影颜色
                //m_Book.ActiveChart.ChartArea.Border.ColorIndex = 8;// 设置整个图表的边框颜色
                wbk.ActiveChart.ChartArea.Border.LineStyle = Excel.XlLineStyle.xlLineStyleNone;//设置边框线条
                wbk.ActiveChart.HasDataTable = false;




                //设置Legend图例的位置和格式
                wbk.ActiveChart.Legend.Top = 20.00; //具体设置图例的上边距
                wbk.ActiveChart.Legend.Left = 60.00;//具体设置图例的左边距
                wbk.ActiveChart.Legend.Interior.ColorIndex = Excel.XlColorIndex.xlColorIndexNone;
                wbk.ActiveChart.Legend.Width = 150;
                wbk.ActiveChart.Legend.Font.Size = 9.5;
                //m_Book.ActiveChart.Legend.Font.Bold = true;
                wbk.ActiveChart.Legend.Font.Name = "Times New Roman";
                //m_Book.ActiveChart.Legend.Position = Excel.XlLegendPosition.xlLegendPositionTop;//设置图例的位置
                wbk.ActiveChart.Legend.Border.LineStyle = Excel.XlLineStyle.xlLineStyleNone;//设置图例边框线条
                //Excel.Axis categoryAxis = (Excel.Axis)wbk.ActiveChart.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary);
                //categoryAxis.CategoryType = Excel.XlCategoryType.xlCategoryScale;//X轴为分类模式 
            }
            catch (System.Exception ex)
            {
                SPCLog.GetInstance().WriteSYS(ex.ToString());
            }
        }
    }


    public class ExcelCell : Object
    {
        private Excel.Worksheet oSheet;
        public Excel.Worksheet Sheet
        {
            get { return oSheet; }
            set { oSheet = value; }
        }


        public ExcelCell(Excel.Worksheet oSheet)
        {
            this.oSheet = oSheet;
        }


        public object this[int startrow, int rowNum, int colNum]
        {
            set
            {
                if (oSheet == null)
                    throw new InvalidOperationException(
                        "Excel.Worksheet reference is null.");
                Excel.Range c1 = (Excel.Range)oSheet.Cells[startrow, 1];
                Excel.Range c2 = (Excel.Range)oSheet.Cells[startrow + rowNum - 1, colNum];
                Excel.Range range = oSheet.get_Range(c1, c2);
                range.Value2 = value;
            }
        }


        public void SetRowRange(int row, int colNum, object[] arr)
        {


            if (oSheet == null)
                throw new InvalidOperationException(
                    "Excel.Worksheet reference is null.");
            Excel.Range c1 = (Excel.Range)oSheet.Cells[row, 1];
            Excel.Range c2 = (Excel.Range)oSheet.Cells[row, colNum];
            Excel.Range range = oSheet.get_Range(c1, c2);
            range.Value2 = arr;
        }


        public object this[int row, int column]
        {
            get
            {
                if (oSheet == null)
                    throw new InvalidOperationException(
                    "Excel.Worksheet reference is null.");
                return ((Excel.Range)oSheet.Cells[row, column]).Value2;
            }
            set
            {
                if (oSheet == null)
                    throw new InvalidOperationException(
                        "Excel.Worksheet reference is null.");
                ((Excel.Range)oSheet.Cells[row, column]).Value2 = value;
            }
        }


        public Excel.Range GetRange(int row, int column)
        {
            if (oSheet == null)
                throw new InvalidOperationException(
            "Excel.Worksheet reference is null.");
            return ((Excel.Range)oSheet.Cells[row, column]);
        }


        public Excel.Range GetRange(int row, int column, ref float top, ref float left)
        {
            Excel.Range rg = GetRange(row, column);


            left = Convert.ToSingle(rg.Left);
            top = Convert.ToSingle(rg.Top);


            return rg;
        }


        public void SetRowHeight(int rowNum, int rowheight)
        {
            ((Range)oSheet.Rows[rowNum, Missing.Value]).RowHeight = rowheight;
        }


        public void SetColWidth(int rowNum, int colWidth)
        {
            ((Range)oSheet.Rows[rowNum, Missing.Value]).ColumnWidth = colWidth;
        }


        public void SetColWidth(int row, int col, int width)
        {
            ((Range)oSheet.Cells[row, col]).ColumnWidth = width;


        }
    }


    /// <summary>
    /// 线样式
    /// </summary>
    public enum LineStyle
    {
        A = 1,
        B = -4115,
        C = 4,
        D = 5,
        E = -4118,
        F = -4119,
        G = -4142,
        H = 13




        //连续直线 = 1,
        //短线 = -4115,
        //线点相间 = 4,
        //短线间两点 = 5,
        //点 = -4118,
        //双线 = -4119,
        //无 = -4142,
        //少量倾斜点 = 13
    }


    /// <summary>
    /// 线粗
    /// </summary>
    public enum BorderWeight
    {
        A = 1,
        B = 2,
        C = -4138,
        D = 4




        //极细 = 1,
        //细 = 2,
        //粗 = -4138,
        //极粗 = 4
    }


    /// <summary>
    /// 常用颜色定义,对就Excel中颜色名
    /// </summary>
    public enum ColorIndex
    {
        A = -4142,
        B = -4105,
        C = 1,
        D = 53,
        E = 52,
        F = 51,
        G = 49,
        H = 11,
        J = 55,
        K = 56,
        L = 9,
        O = 46,
        M = 12,
        N = 10,
        P = 14,
        Q = 5,
        U = 47,
        Y = 16,
        W = 3,
        X = 45,
        AA = 43,
        V = 50,
        AB = 42,
        AC = 41,
        AD = 13,
        AE = 48,
        AF = 7,
        AH = 44,
        AJ = 6,
        AG = 4,
        AL = 8,
        AN = 33,
        AM = 54,
        AQ = 15,
        AW = 38,
        AR = 40,
        AT = 36,
        AY = 35,
        AU = 34,
        AI = 37,
        AO = 39,
        AP = 2


        //无色 = -4142,
        //自动 = -4105,
        //黑色 = 1,
        //褐色 = 53,
        //橄榄 = 52,
        //深绿 = 51,
        //深青 = 49,
        //深蓝 = 11,
        //靛蓝 = 55,
        //灰色80 = 56,
        //深红 = 9,
        //橙色 = 46,
        //深黄 = 12,
        //绿色 = 10,
        //青色 = 14,
        //蓝色 = 5,
        //蓝灰 = 47,
        //灰色50 = 16,
        //红色 = 3,
        //浅橙色 = 45,
        //酸橙色 = 43,
        //海绿 = 50,
        //水绿色 = 42,
        //浅蓝 = 41,
        //紫罗兰 = 13,
        //灰色40 = 48,
        //粉红 = 7,
        //金色 = 44,
        //黄色 = 6,
        //鲜绿 = 4,
        //青绿 = 8,
        //天蓝 = 33,
        //梅红 = 54,
        //灰色25 = 15,
        //玫瑰红 = 38,
        //茶色 = 40,
        //浅黄 = 36,
        //浅绿 = 35,
        //浅青绿 = 34,
        //淡蓝 = 37,
        //淡紫 = 39,
        //白色 = 2
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值