using System;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Windows.Forms;
using System.Runtime.InteropServices; public class Class2
{
private string filePath;
private Excel.Application app = null;
private Workbook wb;
private Worksheet ws;
private Workbooks wbs;
/// <summary>
/// 创建
/// </summary>
/// <param name="filePath"></param>
public void create(string filePath)
{
this.filePath = filePath;
app = new Excel.Application();
if (app == null)
{
Out.show("无法创建Excel对象,可能您的电脑未安装Excel!");
app.Quit();
GC.Collect();
return;
}
try
{
app.Visible = false;
app.Application.DisplayAlerts = false;
}
catch (Exception e)
{ }
wbs = app.Workbooks;
wb = wbs.Add(Excel.XlWBATemplate.xlWBATWorksheet);
ws = wb.Worksheets[] as Worksheet;
} public void Write()
{
ws.Cells[, ] = "hello";
}
/// <summary>
/// 保存文件
/// </summary>
public void Save()
{
while (File.Exists(filePath))
{
try
{
File.Delete(filePath);
break;
}
catch (Exception e)
{
if (MessageBox.Show("该文件已被占用,请关闭", "提示", MessageBoxButtons.RetryCancel)
!= DialogResult.Retry)
{
break;
};
}
}
wb.Saved = true;
wb.SaveAs(filePath);
Kill();
}
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
/// <summary>
/// 杀掉对应进程,释放资源
/// </summary>
public void Kill()
{
int processId;
GetWindowThreadProcessId(new IntPtr(app.Hwnd), out processId);
Out.show(processId.ToString());
System.Diagnostics.Process.GetProcessById(processId).Kill();
}
}

最新文章

  1. Java中Comparable与Comparator的区别
  2. linux的whoami, who指令
  3. Linux Buffer I/O error on device dm-4, logical block
  4. Ubuntu下安装php7后无法启动Apache
  5. scala言语基础学习十二
  6. HDU 4627 The Unsolvable Problem 2013 Multi-University Training Contest 3
  7. Struts1与Struts2的异同
  8. 定制ckeditor的菜单
  9. Android开发——diglog cancel与dismiss方法区别
  10. 利用JAVA多线程来提高数据处理效率
  11. mysql时间比较
  12. dockerfile语法规则
  13. (转)Spring Boot (十九):使用 Spring Boot Actuator 监控应用
  14. Safecracker-HDU1015
  15. 一行一行读Java源码——LinkedBlockingQueue
  16. myeclipse解决Fail to create the java Virtual Machine
  17. MediaPlayer音乐播放器、上一首、下一首、播放、停止、自动下一首、进度条
  18. 【MAVEN】Missing artifact jdk.tools:jdk.tools:jar:1.6 eclipse
  19. Flink的keyby延时源码
  20. 从Java开发者的视角解释JavaScript

热门文章

  1. javaSE基础(二)
  2. Python Flask Web 框架入门
  3. 2-3 Vue实例中的数据,事件和方法
  4. CodeForces 632C Grandma Laura and Apples (模拟)
  5. bzoj 3733: [Pa2013]Iloczyn【dfs】
  6. bzoj 1754: [Usaco2005 qua]Bull Math【高精乘法】
  7. js 本地存储 localStorage 之 angular
  8. (分治)51NOD 1019 逆序数
  9. vue-router+webpack线上部署时单页项目路由,刷新页面出现404问题
  10. hdu6195 cable cable cable