原创: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是我写的一个操作excel的类 http://www.cnblogs.com/emanlee/archive/2007/05/31/766520.html 。
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是我写的一个操作excel的类 http://www.cnblogs.com/emanlee/archive/2007/05/31/766520.html 。