c#操作excel文件模板

来源:互联网 发布:mac os x 10.11黑苹果 编辑:程序博客网 时间:2024/05/29 10:16

创建对象:

CBExcel exUtil = new CBExcel();


函数调用:

 if (!File.Exists(PublicValue.ExcelPath))

 {
      exUtil.Create(PublicValue.ExcelPath, "list", "backup", "other");
      exUtil.Open(PublicValue.ExcelPath, "list");
      exUtil.SetValue(1, 1, PublicValue.StockReservd);

      exUtil.CloseAndSave();}


程序模板:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.IO;

namespace MouseAction
{
    public class CBExcel
    {
        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;
        public Microsoft.Office.Interop.Excel.Application xlsApp = null;
        public Microsoft.Office.Interop.Excel.Workbook workbook = null;
        public Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
        public string str_this_path = null;
        public string str_this_sheet = null;  


        public CBExcel()
        {
        }


        public void Open(string str_path, string str_sheet)
        {
            str_this_path = str_path;
            str_this_sheet = str_sheet;
            xlsApp = new Microsoft.Office.Interop.Excel.Application();
            workbook = xlsApp.Workbooks.Open(str_path, 0, true, 5,
                     System.Reflection.Missing.Value,
                     System.Reflection.Missing.Value,
                     false, System.Reflection.Missing.Value,
                     System.Reflection.Missing.Value, true,
                     false, System.Reflection.Missing.Value,
                     false, false, false);
            worksheet = (Worksheet)workbook.Worksheets[str_sheet];
        }



        public void CloseAndSave()
        {
            xlsApp.DisplayAlerts = false;
            xlsApp.AlertBeforeOverwriting = false;

            if (File.Exists(str_this_path))
            {
                File.Delete(str_this_path);
            }

            xlsApp.ActiveWorkbook.SaveCopyAs(str_this_path);
            xlsApp.Quit();
            xlsApp = null;
            workbook = null;
            worksheet = null;
            str_this_path = null;
        }



        public void SetValue(int row, int col, string str_value)
        {
            if (row <= 0 || col <= 0 || str_value == null)
                throw new Exception("参数不合法");


            worksheet.Cells[row, col] = str_value;
        }




        public string GetValue(int row, int col)
        {
            if (row <= 0 || col <= 0)
                throw new Exception("参数不合法");


            Range myRange = null;
            myRange = worksheet.get_Range(worksheet.Cells[row, col], worksheet.Cells[row, col]);
            string str = myRange.Text.ToString();
            return str;
        }




        public void Create(string BookName, string SheetName1, string SheetName2, string SheetName3)
        {
            Application xlap = new Application();
            xlap.Visible = true;
            Workbook wkbk = xlap.Workbooks.Add();
            Worksheet wkst1 = (Excel.Worksheet)wkbk.Sheets.Add();
            wkst1.Name = SheetName1;
            Worksheet wkst2 = (Excel.Worksheet)wkbk.Sheets.Add();
            wkst2.Name = SheetName2;
            Worksheet wkst3 = (Excel.Worksheet)wkbk.Sheets.Add();
            wkst3.Name = SheetName3;
            wkbk.SaveAs(BookName);
            wkbk.Close();
            xlap.Quit();            
        }
    }
}


原创粉丝点击