namespace wpfandExcel
{
///
/// Window1.xaml 的交互逻辑
///
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
//声明变量
Excel.Application objApp;
Excel._Workbook objBook;
//定义路径
//
//wpf中路径的设定
public string Excelpath = AppDomain.CurrentDomain.BaseDirectory + "info.xls";
///
/// 执行将数值输入到excel表并进行打印
///
public void OpenExcel()
{
//创建Excel控件对象
Excel.Workbooks objBooks;
Excel.Sheets objSheets;
Excel._Worksheet objSheet;
Excel.Range range;
try
{
// Instantiate Excel and start a new workbook.
objApp = new Excel.Application();//启动程序
objBooks = objApp.Workbooks;
//启动工作模板
objBook = objBooks.Add(Excelpath);
objSheets = objBook.Worksheets;
objSheet = (Excel._Worksheet)objSheets.get_Item(1);
#region 将数值导出Excel表
//
//姓名
range = objSheet.get_Range("A2", "A2");
range.Value2 = this.textBox1.Text; //+ " 号";
//
//年龄
range = objSheet.get_Range("B2", "B2");
range.Value2 = this.textBox2.Text;
//
# endregion
//
//将excel显示出来
objApp.Visible = true;
objApp.UserControl = true;
//打印预览
objSheet.PrintPreview(true);
//objSheet.
//隐藏Excel窗口
//objApp.Visible = false;
}
catch (Exception theException)
{
String errorMessage;
errorMessage = "Error: ";
errorMess
1