C#操作WPS表格
方法1、后期绑定动态调用。通过System.Type
动态创建对象,避免直接依赖 COM 引用:
using System;
using System.Runtime.InteropServices;
class Program
{
static void Main()
{
object wpsApp = Activator.CreateInstance(Type.GetTypeFromProgID("Ket.Application"));
dynamic excel = wpsApp;
excel.Visible = true;
dynamic workbook = excel.Workbooks.Add();
dynamic sheet = workbook.Sheets[1];
sheet.Cells[1, 1].Value = "Hello WPS!";
workbook.SaveAs("C:\\Test.xlsx");
workbook.Close();
excel.Quit();
Marshal.ReleaseComObject(excel);
}
}
方法2:在C#中通过COM操作WPS Excel文件时,需引用WPS安装目录下的etapi.dll
文件,并使用对应的ProgID创建实例。
3、截图例子:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;
class Program
{
[STAThread] // 剪贴板操作需要STA线程
static void Main()
{
try
{
// 创建Excel/WPS实例
object wpsApp = Activator.CreateInstance(Type.GetTypeFromProgID("Excel.Application") ??
Type.GetTypeFromProgID("Ket.Application"));
dynamic excel = wpsApp;
excel.Visible = true; // 必须可见才能截图
// 打开指定的Excel文件
string filePath = @"c:\1.xls";
dynamic workbook = excel.Workbooks.Open(filePath);
dynamic sheet = workbook.Sheets[1];
// 选择要截图的区域(A1到P25)
dynamic range = sheet.Range("A1:P25");
range.Select();
// 复制为图片到剪贴板
range.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlBitmap);
// 从剪贴板获取图片
if (Clipboard.ContainsImage())
{
Image img = Clipboard.GetImage();
// 保存图片到C盘
string savePath = @"C:\ExcelScreenshot.png";
img.Save(savePath, ImageFormat.Png);
Console.WriteLine($"截图已保存至:{savePath}");
}
// 清理资源
workbook.Close(false);
excel.Quit();
Marshal.ReleaseComObject(range);
Marshal.ReleaseComObject(sheet);
Marshal.ReleaseComObject(workbook);
Marshal.ReleaseComObject(excel);
}
catch (Exception ex)
{
Console.WriteLine($"错误发生:{ex.Message}");
}
finally
{
// 强制垃圾回收
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
// 需要添加的COM引用
public enum XlPictureAppearance
{
xlScreen = 1,
xlPrinter = 2
}
public enum XlCopyPictureFormat
{
xlBitmap = 2,
xlPicture = -4147
}
4、截图例子:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;
class Program
{
[STAThread]
static void Main()
{
try
{
// 创建WPS实例
object wpsApp = Activator.CreateInstance(Type.GetTypeFromProgID("Ket.Application"));
dynamic excel = wpsApp;
excel.Visible = true; // 必须可见才能截图
// 打开现有工作簿
dynamic workbook = excel.Workbooks.Open(@"C:\1.xls");
dynamic sheet = workbook.Sheets[1]; // 获取第一个工作表
// 选择要截图的区域(A1到P25)
dynamic range = sheet.Range("A1:P25");
range.Select();
// 复制为图片到剪贴板
range.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlBitmap);
// 从剪贴板获取图片
if (Clipboard.ContainsImage())
{
Image img = Clipboard.GetImage();
// 保存图片到C盘
string savePath = @"C:\ExcelScreenshot2.png";
img.Save(savePath, ImageFormat.Png);
Console.WriteLine($"截图已保存至:{savePath}");
}
// 清理资源
workbook.Close(false);
excel.Quit();
Marshal.ReleaseComObject(range);
Marshal.ReleaseComObject(sheet);
Marshal.ReleaseComObject(workbook);
Marshal.ReleaseComObject(excel);
}
catch (Exception ex)
{
Console.WriteLine($"错误发生:{ex.Message}");
}
finally
{
// 强制垃圾回收
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
// 需要添加的COM引用
public enum XlPictureAppearance
{
xlScreen = 1,
xlPrinter = 2
}
public enum XlCopyPictureFormat
{
xlBitmap = 2,
xlPicture = -4147
}
5、
using System;
class Program
{
static void Main()
{
try
{
Type excelType = Type.GetTypeFromProgID("KWPS.Application"); // WPS表格的ProgID
if (excelType != null)
{
dynamic excelApp = Activator.CreateInstance(excelType);
excelApp.Visible = true; // 让WPS表格可见,以便观察
Console.WriteLine("WPS Excel COM 对象创建成功!按Enter退出...");
Console.ReadLine();
excelApp.Quit(); // 关闭WPS
}
else
{
Console.WriteLine("未找到 WPS Excel COM 组件注册。");
}
}
catch (Exception ex)
{
Console.WriteLine($"出错: {ex.Message}");
}
}
}