如何从Asp.Net中的Excel读取数据

时间:2012-01-13 13:17:30

标签: asp.net excel dataset

我正在尝试从上传的xls中读取数据。我使用过这段代码:

OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(fileName) + ";Extended Properties=Excel 8.0");
                            if (connection.State == ConnectionState.Closed)
                                    connection.Open();
                            string query = "select * from [Sheet1$]";
                            OleDbDataAdapter da = new OleDbDataAdapter(query, connection);
                            DataSet ds = new DataSet();
                            da.Fill(ds);

但是我收到了这个错误:外部表不是预期的格式。

我很确定我正在给出正确的道路。我在工作而事实并非如此。如果它工作,那么它不会填充数据表。它给出了一个错误,指出无法找到Sheet1 $对象。有什么帮助吗?

4 个答案:

答案 0 :(得分:1)

你确定excel版本是否正确?您可能还希望将扩展属性包装在引号中。我还建议您清理资源,这样就不会造成内存泄漏。

var path = Server.MapPath(fileName);

//where 8.0 may be a different version: 9 - 12?
var connectionString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended ""Properties=Excel 8.0""", path);
using(var connection = new OleDbConnection(connectionString))
using(var command = connection.CreateCommand())
{
     connection.Open();
     command.CommandText = "select * from [Sheet1$]";
     var table = new DataTable();
     using(var reader = command.ExeucteReader())
     {
        table.Load(reader);
        return table;
     }
}

答案 1 :(得分:0)

看一下这个帖子: Excel "External table is not in the expected format."

也许你应该按照他们的建议改变提供者。

另一个选择是使用OpenXML SDK来读取excel文件。这可以让你开始: http://msdn.microsoft.com/en-us/library/ff921204.aspx

答案 2 :(得分:0)

string savePath = "/Assets/UploadedFiles/";
                            string fileName = "Activity.xls";
                            savePath += fileName; 
OleDbConnection conn= new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath(savePath) + ";Extended Properties='Excel 12.0;HDR=YES'");
    if (conn.State == ConnectionState.Closed)
    conn.Open();
    string query = "select * from [Sheet1$]";
      OleDbDataAdapter da = new OleDbDataAdapter(query, conn);
                                DataSet ds = new DataSet();
                                da.Fill(ds, "Activities");
                                dt = ds.Tables[0];
                                conn.Close();

答案 3 :(得分:-1)

将文件保存在硬盘中,然后添加对 Microsoft Excel 12.0对象库的引用并声明使用:

using Excel = Microsoft.Office.Interop.Excel;

然后实例化一个类并加载文件以读取单元格值,例如:

Excel.Application xlApp ;
Excel.Workbook xlWorkBook ;
Excel.Worksheet xlWorkSheet ;
object misValue = System.Reflection.Missing.Value;

xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Open("file.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

MessageBox.Show(xlWorkSheet.get_Range("A1","A1").Value2.ToString());

xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();

编辑1: 如果您不想在服务器中安装Office,则可以使用与{作者页面)类似的简单工作的excellibrary

// open xls file
Workbook book = Workbook.Load(file);
Worksheet sheet = book.Worksheets[0];

 // traverse cells
 foreach (Pair<Pair<int, int>, Cell> cell in sheet.Cells)
 {
     dgvCells[cell.Left.Right, cell.Left.Left].Value = cell.Right.Value;
 }

 // traverse rows by Index
 for (int rowIndex = sheet.Cells.FirstRowIndex; 
        rowIndex <= sheet.Cells.LastRowIndex; rowIndex++)
 {
     Row row = sheet.Cells.GetRow(rowIndex);
     for (int colIndex = row.FirstColIndex; 
        colIndex <= row.LastColIndex; colIndex++)
     {
         Cell cell = row.GetCell(colIndex);
     }
 }