使用WPF写入Excel文件 [英] Write to an Excel File using WPF

查看:316
本文介绍了使用WPF写入Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计,



有一个令人不安的疑问,希望CP的导师能够提供帮助。

我有一个简单的WPF应用程序,其中有一个按钮。

该按钮应该将数据写入excel表,这不会发生。



我在构建应用程序时遇到错误:



''Window''是''Systems.Windows.Window'和''之间的模糊引用Microsoft.Office.Interop.Excel.Window''。



以下是我的Window1.xaml代码:



Hi Folks,

Have a disturbing doubt, hope mentors at CP would be able to assist.
I have a simple WPF application, in which there is a button.
The button is supposed to write data to an excel sheet, which ain''t happening.

I get an error while building the application that:

''Window'' is an ambiguous reference between ''Systems.Windows.Window'' and ''Microsoft.Office.Interop.Excel.Window''.

Below is my Window1.xaml code:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="406" Width="345">
    <Grid>
        <Image Margin="12,100,66,12" Name="image1" Stretch="UniformToFill" Source="Images\s3.jpg" />
        <Button Height="23" Margin="0,38,100,0" Name="btnExcel" VerticalAlignment="Top" BorderBrush="Chartreuse" Foreground="DarkMagenta" HorizontalAlignment="Right" Width="75" Click="btnExcel_Click">Excel</Button>
    </Grid>
</Window>





以下是我的Window1.xaml.cs代码:





Below is my Window1.xaml.cs code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Office.Interop.Excel;
using System.Reflection;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void btnExcel_Click(object sender, RoutedEventArgs e)
        {
            //creating an excel application
            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
            if (excelApp == null)
            {
                MessageBox.Show("Check whether you've added Excel assembly as reference");
            }
            excelApp.Visible = true;

            //creating an object of the workbook
            Workbook wbobj = excelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);

            //adding a worksheet
            Worksheet ws = (Worksheet)wbobj.Worksheets[1];

            //defining the range of the worksheet
            Range sheetRange = ws.get_Range("B1", "B5");

            //creating elements with an array
            int[] array = new int[5];
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = i * 2;
            }
            Object[] obj = new Object[1];
            obj[0] = array;
            sheetRange.GetType().InvokeMember("Value",BindingFlags.SetProperty,null,sheetRange,obj);

        }
    }
}





以上代码完全正常通过普通的Windows窗体完成,因为使用的主命名空间是Form。这里没有任何含糊之处。

任何人都可以对这种情况有所了解,是否有可能在WPF应用程序中调用excel内容?

任何帮助都是非常感谢。



问候

Anurag



The above code works perfectly fine when it''s done via normal Windows Forms, because the master namespace used is Form. There is no ambiguity here.
Can anybody shed some light on this scenario, as to is it possible to invoke an excel stuff in a WPF application?
Any assistance would be highly appreciated.

Regards
Anurag

推荐答案

至少有三种方法:



There are at least three approaches:

  1. 不要写

  1. Don''t write
using Systems.Windows.Window;






and

using Microsoft.Office.Interop.Excel;



。跳过其中一行或两者。使用命名空间时,请使用完全限定名称,例如


in the same file. Skip on one of those lines, or both. When a namespace is used, use fully-qualified names, such as

Microsoft.Office.Interop.Excel.Window myWindow = //...



  • Excel生成完全是与WPF无关。您只导出数据,从不导出任何WPF UI元素。也就是说,你真的应该分离Excel生成的东西和WPF代码文件。将它们全部写在单独的文件中,不仅仅是因为您的问题,还要使您的应用程序可维护。

  • 使用简单的别名语法:


  • Excel generation is totally unrelated to WPF. You only export data, never any WPF UI elements. That said, you really should separate Excel generation stuff and WPF code files. Write it all in separate files, and not only because of your problem, but to make your application maintainable.
  • Use the simple alias syntax:

    using Systems.Windows.Window;
    // using Microsoft.Office.Interop.Excel // remove, don't use it; instead, use:
    using ExcelWindow = Microsoft.Office.Interop.Excel.Window;
    
    //...
    
    ExcelWindow myWindow = //...







  • 更喜欢什么?在您的所有练习中,您将需要使用所有这些技巧。至于你的特殊情况,即使你使用#1和#3,你仍然需要尽力隔离代码的WPF和Excel部分,所以即使你使用与类型相关的其他技术,#2也是必须的。命名。



    -SA


    这篇关于使用WPF写入Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆