c#操作excel文件模板

创建对象:

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();            
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值