-1

我的服务器没有任何 Microsoft Office,我不想安装 Microsoft Office。

当我使用此代码读取 Excel 2007 文件时

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myOldExcelFile.xls;
Extended Properties="Excel 8.0;HDR=YES";

它无法读取 Excel 文件。有谁知道出了什么问题?

4

4 回答 4

4

您可以使用EPPlus

EPPlus 是一个 .net 库,它使用 Open Office Xml 格式 (xlsx) 读取和写入 Excel 2007/2010 文件。

不必安装 MS Office。

打开的示例脚本.xlsx

using OfficeOpenXml;
// Get the file we are going to process
var existingFile = new FileInfo(filePath);
// Open and read the XlSX file.
using (var package = new ExcelPackage(existingFile))
{
    // Get the work book in the file
    ExcelWorkbook workBook = package.Workbook;
    if (workBook != null)
    {
        if (workBook.Worksheets.Count > 0)
        {
            // Get the first worksheet
            ExcelWorksheet currentWorksheet = workBook.Worksheets.First();

            // read some data
            object col1Header = currentWorksheet.Cells[0, 1].Value;
            ...

代码示例来自:http ://blog.fryhard.com/archive/2010/10/28/reading-xlsx-files-using-c-and-epplus.aspx

于 2013-02-26T05:14:42.927 回答
1

很高兴看到您没有在服务器上安装 Office 并遵循 Office服务器端自动化的注意事项

确保您在服务器上安装了Microsoft Access Database Engine Redistributable,这是针对 Office 2010 (v14) 的,2007 (v12) 应该很容易找到。

这是:2007 Office System 驱动程序:数据连接组件

于 2013-02-26T05:11:00.293 回答
0

//启动excel读取有问题的excel文件

示例代码将帮助您实现这一目标。

  Microsoft.Office.Interop.Excel.Application ExcelObj = null;

    ExcelObj = new Microsoft.Office.Interop.Excel.Application();
    if (ExcelObj == null)

    {

     MessageBox.Show("ERROR: EXCEL couldn't be started!");

     System.Windows.Forms.Application.Exit();

    }


    Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(openFileDialog.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing,

    Type.Missing, Type.Missing, Type.Missing, Type.Missing,

    Type.Missing, Type.Missing, Type.Missing, Type.Missing,

    Type.Missing, Type.Missing);


    Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets;

    Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1);


    for(int x = 1; x <= 29; x++)

    {

    Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range("A"+x.ToString(), "I" + x.ToString());

    System.Array myvalues = (System.Array)range.Cells.get_Value(range.);

    string[] strArray = ConvertToStringArray(myvalues);

    }
于 2013-02-26T05:18:14.860 回答
0

您可以.dll从 microsoft 站点下载并在您的项目中使用它。
你不需要在那里安装microsoft excel。

您可以使用 ado.net 阅读它

    var myConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;
       Data Source='c:\\Language_Batch1_OneClick.xls';Extended Properties=Excel 8.0;");
    var myCommand = new OleDbCommand();
    var upCommand = new OleDbCommand();
    int i = 0;
    try
    {

        string sql = null;
        myConnection.Open();
        myCommand.Connection = myConnection;
        sql = "select ANSWER_CODE,Punjabi from [Batch_Lang_1$]";
        myCommand.CommandText = sql;
        var dataReader = myCommand.ExecuteReader();

        while (dataReader.Read())
        {
            var langText = dataReader["Punjabi"].ToString();
        }
于 2013-02-26T05:35:07.550 回答