windows窗体(winform)中嵌入显示Excel工作表。

环境:c#2005,Excel2003.
1 实现后的效果如下图:
(截图)
2 实现代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Owc11;

namespace CityToProvince.GUILayer
{
public partial class frmExcel : Form
{
public frmExcel()
{
InitializeComponent();
}

private void buttonOpen_Click(object sender, EventArgs e)
{
OpenFileDialog myOpenFileDialog = new OpenFileDialog();
myOpenFileDialog.Filter = "Excel文件(*.xls)|*.xls|xml格式表格(*.xml)|*.xml";
myOpenFileDialog.FilterIndex = 2;
myOpenFileDialog.Title = "选择xml文件";
myOpenFileDialog.InitialDirectory = "c:\\";
myOpenFileDialog.RestoreDirectory = true;
DialogResult dr = myOpenFileDialog.ShowDialog();
if (dr == DialogResult.OK)
{
try
{
this.axSpreadsheetExcel.XMLURL = myOpenFileDialog.FileName;
}
catch
{
MessageBox.Show("错误");
}
}
}

private void buttonConvert_Click(object sender, EventArgs e)
{//xls->xml and open
OpenFileDialog myOpenFileDialog = new OpenFileDialog();
myOpenFileDialog.Filter = "Excel文件(*.xls)|*.xls";
myOpenFileDialog.FilterIndex = 1;
myOpenFileDialog.Title = "选择Excel文件";
myOpenFileDialog.InitialDirectory = "c:\\";
myOpenFileDialog.RestoreDirectory = true;
DialogResult dr = myOpenFileDialog.ShowDialog();
if (dr == DialogResult.OK)
{
try
{
ExcelProcess.ExcelProcess myExcel = new ExcelProcess.ExcelProcess();
myExcel.OpenFileName = myOpenFileDialog.FileName;
string saveXmlName = myOpenFileDialog.FileName.ToString().Replace("xls", "xml");
if (System.IO.File.Exists(saveXmlName))
{
System.IO.File.Delete(saveXmlName);
}
myExcel.SaveFileName = saveXmlName;
myExcel.OpenExcelFile();
myExcel.SaveExcelAsXML();
myExcel.CloseExcelApplication();

}
catch
{
MessageBox.Show("转换错误");
}
}

}

private void buttonClose_Click(object sender, EventArgs e)
{
//this.axSpreadsheetExcel.XMLURL = null;
this.Close();
}

private void buttonConvertOpen_Click(object sender, EventArgs e)
{
//xls->xml and open
OpenFileDialog myOpenFileDialog = new OpenFileDialog();
myOpenFileDialog.Filter = "Excel文件(*.xls)|*.xls";
myOpenFileDialog.FilterIndex = 1;
myOpenFileDialog.Title = "选择Excel文件";
myOpenFileDialog.InitialDirectory = "c:\\";
myOpenFileDialog.RestoreDirectory = true;
DialogResult dr = myOpenFileDialog.ShowDialog();
if (dr == DialogResult.OK)
{
try
{
ExcelProcess.ExcelProcess myExcel = new ExcelProcess.ExcelProcess();
myExcel.OpenFileName = myOpenFileDialog.FileName;
string saveXmlName = myOpenFileDialog.FileName.ToString().Replace("xls", "xml");
if (System.IO.File.Exists(saveXmlName))
{
System.IO.File.Delete(saveXmlName);
}
myExcel.SaveFileName = saveXmlName;
myExcel.OpenExcelFile();
myExcel.SaveExcelAsXML();
myExcel.CloseExcelApplication();
this.axSpreadsheetExcel.XMLURL = saveXmlName;
}
catch
{
MessageBox.Show("转换错误");
}
}

}

private void buttonSave_Click(object sender, EventArgs e)
{
SaveFileDialog mySaveFileDialog = new SaveFileDialog();
mySaveFileDialog.Title = "导出Excel文件的名称";
mySaveFileDialog.Filter = "Excel文件|*.xls";
string exportExcelFile;
mySaveFileDialog.InitialDirectory = SysCommon.ShareData.ExportExcelPath;
DialogResult mydr = mySaveFileDialog.ShowDialog();
if (mydr == DialogResult.OK)
{
exportExcelFile = mySaveFileDialog.FileName;
this.axSpreadsheetExcel.Export(exportExcelFile, SheetExportActionEnum.ssExportActionNone,
SheetExportFormat.ssExportAsAppropriate);
}
}
}
}
其中ExcelProcess操作类 http://www.cnblogs.com/emanlee/archive/2007/05/31/766520.html

 

摘自:http://www.cnblogs.com/emanlee/archive/2006/02/18/333064.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值