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

}

}

}

相关推荐
sin220121 小时前
Spring事务管理(SpringBoot)
java·spring boot·spring
C***115021 小时前
Spring TransactionTemplate 深入解析与高级用法
java·数据库·spring
BD_Marathon21 小时前
SpringBoot——配置文件格式
java·spring boot·后端
indexsunny21 小时前
互联网大厂Java面试实战:Spring Boot与微服务在电商场景的应用解析
java·spring boot·redis·微服务·kafka·gradle·maven
smileNicky21 小时前
Lombok @Data 在 IDEA 中运行报错解决方案
java·ide·intellij-idea
计算机学姐21 小时前
基于SpringBoot的汉服租赁系统【颜色尺码套装+个性化推荐算法+数据可视化统计】
java·vue.js·spring boot·后端·mysql·信息可视化·推荐算法
qq_54702617921 小时前
Maven 仓库管理
java·maven
天天摸鱼的java工程师21 小时前
线程池深度解析:核心参数 + 拒绝策略 + 动态调整实战
java·后端
mjhcsp21 小时前
C++ KMP 算法:原理、实现与应用全解析
java·c++·算法·kmp
邵伯21 小时前
Java源码中的排序算法(一)--Arrays.sort()
java·排序算法