接下来我们接着新建Excel工作表,在新建Excel文件基础上新增工作表。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Excel;
using System.Diagnostics;
using System.Reflection;
namespace ConsoleApplication3
{
class EditExcel
{
#region 创建工作表
///
/// 创建新的工作表
///
///
public void CreateSheet(string ExcelName)
{
//创建 Excel对象
Application App = new Application();
//获取缺少的object类型值
object missing = Missing.Value;
//打开指定的Excel文件
Workbook openwb = App.Workbooks.Open(ExcelName, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
//添加新的Excel工作表
Worksheet newsheet = (Worksheet)openwb.Worksheets.Add(missing, missing, 1, missing);
//Add参数(Before, After, Count, Type);
Console.WriteLine("请输入需要新增的工作表名称:");
string sheetName = Console.ReadLine();
newsheet.Name = sheetName;
Console.WriteLine("添加成功!");
App.DisplayAlerts = false;//不现实提示对话框
openwb.Save();//保存工作表
App.Visible = true;//显示Excel
openwb.Close(false, missing, missing);//关闭工作表
//创建进程对象
Process[] ExcelProcess = Process.GetProcessesByName("Excel");
//关闭进程
foreach (Process p in ExcelProcess)
{
p.Kill();
}
}
#endregion
}
}
Add后面的参数(Before, After, Count, Type)解释:
Before:在其他工作前面添加工作表
After:在其他工作后面添加工作表
Count:要添加几个工作表
Type:工作表的类型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Excel;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
EditExcel app = new EditExcel();
//app.Create(@"C:\Users\敏\Desktop");
app.CreateSheet(@"C:\Users\敏\Desktop\测试页面20181211021229.xls");
}
}
}
我现在使用控制台写的,所以直接给其赋值绝对路径了,如果你用windows窗口可以动态获取文件路径。
新增Excel工作表查询,如果Excel工作表中已存在新增工作表名时则直接跳过,没有则新增:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Excel;
using System.Diagnostics;
using System.Reflection;
namespace ConsoleApplication3
{
class EditExcel
{
public Microsoft.Office.Interop.Excel.Worksheet ws;
#region 创建工作表
///
/// 创建新的工作表
///
///
public void compute(string ExcelName)
{
//创建进程对象
Process[] ExcelProcess = Process.GetProcessesByName("EXCEL");
//关闭进程
foreach (Process p in ExcelProcess)
{
p.Kill();
}
List sheets = new List(); //存储Excel中的sheet工作表
Application App = new Application(); //创建 Excel对象
object missing = Missing.Value; //获取缺少的object类型值
Workbook openwb = App.Workbooks.Open(ExcelName, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); //打开指定的Excel文件
//--------------------------------获取Excel工作表名称------------------------------
for (int i = 1; i < openwb.Worksheets.Count; i++) //循环sheet工作表
{
string sheet = ((Worksheet)openwb.Worksheets[i]).Name;
sheets.Add(sheet);
}
//--------------------------------定义新增Excel工作表名称------------------------------
string addsheet = "刀具预估数量";
if (sheets.Contains(addsheet)) //判断Excel中是否存在该工作表
{
Console.WriteLine("新增工作表已存在");
}
else //没有则新增该工作表
{
ws = (Worksheet)openwb.Worksheets.Add(missing, missing, 1, missing); //添加新的Excel工作表
ws.Name = addsheet;
openwb.Save();//保存Excel文件
App.DisplayAlerts = false;//不显示提示对话框
//App.Visible = true;
}
}
#endregion
}
}
欢迎关注本人的公众号:编程手札,文章也会在公众号更新