Object Nothing = Missing.Value;
//WdSaveFormat 为Excel文档的保存格式
object format = Excel.XlFileFormat.xlOpenXMLWorkbook;
Excel.XlFileFormat version = Excel.XlFileFormat.xlExcel8;
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "Excel文档(*.xlsx)|*.xlsx|Excel97-2003文档(*.xls)|*.xls";
if (System.Windows.Forms.DialogResult.OK == openFile.ShowDialog())
{
string path = openFile.FileName;//文件路径变量
if (System.IO.Path.GetExtension(path).ToLower() == ".xlsx")
{
version = Excel.XlFileFormat.xlOpenXMLWorkbook;
}
try
{
Excel.Application excelApp = new Excel.Application(); //Excel应用程序变量
Excel.Workbook excelBook = excelApp.Workbooks.Open(path, version, Nothing, Nothing, Nothing, Nothing, Excel.XlSaveAsAccessMode.xlExclusive, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
Excel.Worksheet excelSheet = (Excel.Worksheet)excelApp.ActiveSheet;
Excel.Range test = (Excel.Range)excelSheet.Cells[1, 1];
MessageBox.Show(test.Value2.ToString());
}
catch (Exception ex)
{
MessageBox.Show("Excel创建失败!\n原因:" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
解决方案 »
- c# 即时通讯软件问题
- 使用SubSonic查詢,請大家幫忙看一下問題在那
- c# 代码实现关闭.exe文件问题
- C# Access 顯示秒
- 高分求教:DEVEXPRESS 组件ClickOnce方式发布,客户端报错:“应用程序即将关闭”无法启动,如何解决?
- (高分)C#關於註冊表中的問題:如何判断注册表中的某个键是否存在.
- 请教C#调用动态库问题?熟悉的朋友路过帮忙看看。急~~~
- 网址给DataGrid传递orderby排序 但是在翻页的时候 会丢失排序规则
- 谁做过扫描啊,怎么做啊,急啊,我找组件也找到啊,什么组件啊
- VPF中用宏代换只要5行代码就能实现的功能在C#中要几百行代码???各位有什么好的招?
- 我有一段程序,每执行一步要在窗体向用户显示一句话,用什么事件?
- winform imagelist 中放了两张图,如何调用第二张图
Object Nothing = Missing.Value;
//WdSaveFormat 为Excel文档的保存格式
object format = Excel.XlFileFormat.xlOpenXMLWorkbook;
Excel.XlFileFormat version = Excel.XlFileFormat.xlExcel8;
SaveFileDialog saveexcel = new SaveFileDialog();
saveexcel.Filter = "Excel文档(*.xlsx)|*.xlsx|Excel97-2003文档(*.xls)|*.xls";
if (saveexcel.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.SystemStatus.Text = "数据正在导出中,请稍后...";
string path = saveexcel.FileName;//文件路径变量
//保存为2007的格式
if (System.IO.Path.GetExtension(saveexcel.FileName).ToLower() == ".xlsx")
{
version = Excel.XlFileFormat.xlOpenXMLWorkbook;
} if (System.IO.File.Exists(path))
{
System.IO.File.Delete(path);
}
try
{
Excel.Application excelApp = new Excel.Application(); //Excel应用程序变量
if (null == excelApp)
{
MessageBox.Show("无法创建Excel对象,可能计算机未安装Excel", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Excel.Workbooks excelWorkBooks = excelApp.Workbooks; //Excel文档变量
Excel.Workbook excelWorkBook = excelWorkBooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet excelSheet = (Excel.Worksheet)excelWorkBook.Worksheets[1];
//excelSheet.Name = "test";
excelSheet.Cells[1, 1] = "123";
//将 excelDoc文档对象的内容保存为XLSX文档
excelWorkBook.SaveAs(path, version, Nothing, Nothing, Nothing, Nothing, Excel.XlSaveAsAccessMode.xlExclusive, Nothing, Nothing, Nothing, Nothing, Nothing);
//关闭excelDoc文档对象
excelWorkBook.Close(Nothing, Nothing, Nothing);
//关闭excelApp组件对象
excelApp.Quit();
this.SystemStatus.Text = "";
MessageBox.Show("数据导出成功", "", MessageBoxButtons.OK);
}
catch (Exception ex)
{
MessageBox.Show("Excel创建失败!\n原因:" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
这样就可以了,自己搞定了