C# 用spreadsheet打开excel文件
来源:互联网 发布:网络电视如何看翡翠台 编辑:程序博客网 时间:2024/05/22 07:48
using System.IO;
using Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;
public bool OpenExcelInSpreadSheet()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Excel文件(*.xls)|*.xls|所有文件(*.*)|*.*";
openFileDialog.Title = "选择要打开的文件";
openFileDialog.ShowDialog();
if (openFileDialog.FileName.Length > 0)
{
//将原文件拷贝一份到临时文件夹再打开备份文件,不推荐直接打开原文件,因为有可能该文件已经被打开了。
//也不推荐将excel文件转换成xml文件再用spreadsheet打开,因为有的版本的excel格式转换到xml后不能正确打开。
//本例临时文件夹为应用程序所在目录的“temp excel files”文件夹。
string path = System.Windows.Forms.Application.ExecutablePath;
FileInfo exeInfo = new FileInfo(path);
path = exeInfo.DirectoryName + "\\";
string tempExcel = path + "temp excel files\\" + openFileDialog.FileName.Split(new Char[] { '\\' }).Last();
if (File.Exists(tempExcel))
{
string just_name = ofd.FileName.Split(new Char[] { '\\' }).Last();
DialogResult result = MessageBox.Show(just_name + "已经存在,确认覆盖?", "文件上传提示", MessageBoxButtons.OKCancel);
if (result == DialogResult.OK)
{
File.Copy(ofd.FileName, tempExcel, true);
Excel.Application app = new Excel.Application();
if (app == null)
{
MessageBox.Show("Excel打开失败!");
return false;
}
app.Visible = false;
app.UserControl = true;
Excel.Workbooks workbooks = app.Workbooks;
Excel._Workbook workbook = workbooks.Open(newFileName, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, null, null);
app.DisplayAlerts = false;
try
{
app.CopyObjectsWithCells = true;
Excel._Worksheet sheet1 = (Excel._Worksheet)workbook.Sheets[1]; //打开sheet1
sheet1.UsedRange.Copy(Type.Missing); //复制sheet1到剪贴板
AxMicrosoft.Office.Interop.Owc11.AxSpreadsheet spreadsheet = new AxMicrosoft.Office.Interop.Owc11.AxSpreadsheet();
spreadsheet.get_Range(this.spreadsheet.Cells[1, 1]).Paste(); //将剪贴板复制的内容贴到spreadsheet上
}
catch (System.Exception exc)
{
MessageBox.Show(exc.Message);
return false;
}
app.Quit();
File.Delete(tempExcel);
return true;
}else
{
return false;
}
}
}
}
- C# 用spreadsheet打开excel文件
- C#打开EXCEL文件
- C#不能打开excel文件
- c# 选择excel文件,并打开excel
- 使用spreadsheet插件导出excel文件
- 使用GemBox.Spreadsheet控件操作Excel文件
- [perl] Spreadsheet::WriteExcel 和 Spreadsheet::Read 对 excel 文件的读写
- 用Excel VBA选择文件、打开文件
- 用Excel VBA选择文件、打开文件
- C#窗体打开EXCEL
- c#打开excel
- C# 打开excel
- 使用Spreadsheet操作Excel
- Excel(二)Spreadsheet::WriteExcel
- Excel比较工具SpreadSheet
- C# 打开指定文件夹或者文件 导入Excel
- C#判断已经被打开过的excel文件
- 用Microsoft Excel打开编辑CSV文件
- poj 1845 Sumdiv(二分递归求等比数列+素因子分解)
- 如何让一部全新的 MacBook 快速完成开发环境配置
- 安装Matlab JPEG Toolbox
- 欢迎使用CSDN-markdown编辑器
- LoadRunner脚本中的乱码问题以及解决办法
- C# 用spreadsheet打开excel文件
- 图文介绍Windows系统下使用 Github账户 + msysgit + TortoiseGit 进行文件管理的方法。
- IO流(2)
- Android点击EditText文本框之外任何地方隐藏键盘的解决办法
- Android-数据存储(文件存储)
- 内部类是否设置为static的问题
- Java IO流详解
- 递归和动态规划(1)
- Velocity的应用