[新手]错误:外部表格不是预期格式 [英] [Newbie] Error: External table is not in expected format

查看:87
本文介绍了[新手]错误:外部表格不是预期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个MVC .Net Web应用程序,我想从.xlsx和.xls文件中读取数据。目前,我的应用程序读取.xls文件没有问题。我读到了所有的连接字符串,但是我一直在弄清楚我应该如何实现它们,因为这是我第一次处理C#的经验。无论如何这里是我的代码的一些片段。谢谢你提前很多。体验你的大力帮助。



I'm developing an MVC .Net web application, and I want to read data from both .xlsx and .xls file. Currently, my application have no problem reading .xls file. I read about all the connection string but I have been having problem figuring out how should I go about implementing them as this is my first experience handling C#. Anyway here are some snippets of my code.Thanks alot in advance. Experience your great help.

private void button_OpenFile_Click(object sender, EventArgs e)
        {
            if(fileType.Equals("csv")){
                openFileDialog_ExcelFile.Filter = "CSV Files (.csv)|*.csv";
            }
            else if (fileType.Equals("xls"))
            {
                openFileDialog_ExcelFile.Filter = "Excel Worksheets 1997 -2003(.xls)|*.xls";
            }
            else if (fileType.Equals("xlsx"))
            {
                openFileDialog_ExcelFile.Filter = "Excel Worksheets 2007 (.xlsx)|*.xlsx";
            }


            DialogResult result = openFileDialog_ExcelFile.ShowDialog(); // Show the dialog.

            if (result == DialogResult.OK) // Test result.
            {

                OpenNewFile();
                fileOpened = true;
            }

            if (includeAllAttributesByDefault)
                SelectAndIncludeAll();

            nonNumberChkBx.Checked = includeNumericValuesByDefault;

            //Debug.WriteLine(result); // <-- For debugging use only.
        }













private void OpenNewFile()
    {
        // Initializing new data object.
        try
        {
            data = new MyData(openFileDialog_ExcelFile.FileName);


            UpdateCheckListBox();
            UpdateGridView();
            UpdateCurrentFileInfo();
        }

推荐答案

我认为codeproject在本文中已经为您提供了解决方案:

C#:读取和写入Excel(* .xls和* .xlsx)文件内容没有Excel自动化(使用NPOI和ADO.NET) [ ^ ]



基本上,

,您使用:

I think codeproject already have the solution for you in this article :
C#: Read and Write Excel (*.xls and *.xlsx) Files Content without Excel Automation (using NPOI and ADO.NET)[^]

basically,

for XLS, you use :
conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;" +
                "Data Source=" + Application.StartupPath + "\\test.xls;" +
                "Extended Properties=Excel 8.0");








for XLSX :

conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" +
                    "Data Source=" + Application.StartupPath + "\\test.xlsx;" +
                    "Extended Properties=Excel 12.0 Xml");
                conn.Open();


这篇关于[新手]错误:外部表格不是预期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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