在excel文件中保存数据 -

时间:2011-02-05 19:47:47

标签: datatable export-to-excel savefiledialog excel

我有一个小应用程序,最后用户可以将结果保存在excel文件中。一切看起来都不错,但在此之后几秒钟微软Windows通信:“程序Microsoft Office Excel停止工作”。好的 - 加入它。

如果用户将数据保存为* .xls文件,则打开此文件没有问题 - 保存到此文件中的数据是正确的。

如果用户将数据保存为* .xlsx文件,则会出现问题。当我尝试打开此文件时,显示消息:“程序Microsoft Excel无法打开文件* .xlsx,因为文件格式或扩展名有误。检查文件是否已损坏,文件扩展名是否与其格式正确”

我用来将数据保存为excel文件的代码如下:

public void SaveData(ExcelWriter ew)
    {
        SaveFileDialog saveFD = new SaveFileDialog();

        saveFD.InitialDirectory = "C:\\users\\Documents";
        saveFD.FileName = this._saveExcelFileName;
        saveFD.Filter = "excel 97-03(*.xls)|*.xls|excel 2007 (*.xlsx)|*.xlsx";
        saveFD.FilterIndex = 2;
        saveFD.RestoreDirectory = true;

        if (saveFD.ShowDialog() == DialogResult.OK)
        {
            try
            {
                this._saveExcelFileName = saveFD.FileName;
                ew.RunExcelWriter(_dt, _saveExcelFileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }


    public void RunExcelWriter(DataTable DataT, string fileName)
    {
        StartExcel();
        GetANewWorkbook();
        GetTheActiveSheet();
        ProcessTheDataTable(DataT);
        SaveTheSheet(fileName);
        Clean();
    }

private void StartExcel()
    {
        // Start Excel and get Application object.
        oXL = new Excel.Application();
        // Set some properties
        oXL.Visible = true;
        oXL.DisplayAlerts = false;
    }

    private void GetANewWorkbook()
    {
        // Get a new workbook.
        oWB = oXL.Workbooks.Add(Missing.Value);
    }

    private void GetTheActiveSheet()
    {
        oSheet = (Excel.Worksheet)oWB.ActiveSheet;
        oSheet.Name= "Wyniki";
    }

    private void ProcessTheDataTable(DataTable DT)
    {

        int rowCount = 1;
        foreach (DataRow dr in DT.Rows)
        {
            rowCount += 1;
            for (int i = 1; i < DT.Columns.Count + 1; i++)
            {
                // Add the header the first time through
                if (rowCount == 2)
                {
                    oSheet.Cells[1, i] = DT.Columns[i - 1].ColumnName;

                }
                oSheet.Cells[rowCount, i] = dr[i - 1].ToString();
            }
        }
        // Resize the columns
        oRange = oSheet.get_Range(oSheet.Cells[1, 1], oSheet.Cells[rowCount, DT.Columns.Count]);
        oRange.EntireColumn.AutoFit();
        //oRange.Style = oSheet.Cells.Style;      
    }

    private void SaveTheSheet(string FN)
    {
        oSheet = null;
        oRange = null;
        oWB.SaveAs(FN, Excel.XlFileFormat.xlWorkbookNormal,
            Missing.Value, Missing.Value, Missing.Value, Missing.Value,
            Excel.XlSaveAsAccessMode.xlShared,
            Missing.Value, Missing.Value, Missing.Value,
            Missing.Value, Missing.Value);

        if(oWB.Saved==true)
        {
            MessageBox.Show("Plik został zapisany pomyślnie");
        }
        else
        {
            MessageBox.Show("PLIKU NIE ZAPISANO");
        }

        oWB.Close(Missing.Value, Missing.Value, Missing.Value);
        oWB = null;
        oXL.Quit();
    }

如果有人知道为什么会这样? 非常感谢提前

更新1:

建议的答案不起作用。我改为:

private void SaveTheSheet(string FN)
    {
        oSheet = null;
        oRange = null;
        oWB.SaveAs(FN, Excel.XlFileFormat.xlXMLSpreadsheet,
            Missing.Value, Missing.Value, Missing.Value, Missing.Value,
            Excel.XlSaveAsAccessMode.xlShared,
            Missing.Value, Missing.Value, Missing.Value,
            Missing.Value, Missing.Value);

        if(oWB.Saved==true)
        {
            MessageBox.Show("Plik został zapisany pomyślnie");
        }
        else
        {
            MessageBox.Show("PLIKU NIE ZAPISANO");
        }

        oWB.Close(Missing.Value, Missing.Value, Missing.Value);
        oWB = null;
        oXL.Quit();
    }

现在,当我保存时,有一个meesage:

“HRESULT异常:0x800A03EC

更新2:

昨天我有点挣扎,在将数据写入.xlsx格式时,我设法获得了预期的效果。保存到.xls时,我仍然收到Microsoft Windows消息以关闭excel应用程序,但至少(在另一个警告批准后)我可以打开此文件。 下面我用来保存为.xlsx格式的代码:

oWB.SaveAs(FN, Excel.XlFileFormat.xlWorkbookDefault,
                Missing.Value, Missing.Value, false, false,
                Excel.XlSaveAsAccessMode.xlNoChange,
                Missing.Value, Missing.Value, Missing.Value,
                Missing.Value, Missing.Value);

这是.xls

                oWB.SaveAs(FN, Excel.XlFileFormat.xlXMLSpreadsheet,
                Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                Excel.XlSaveAsAccessMode.xlNoChange,
                Missing.Value, Missing.Value, Missing.Value,
                Missing.Value, Missing.Value);

请 - 任何建议都很重要。

2 个答案:

答案 0 :(得分:2)

就我而言,aslo作品Excel.XlFileFormat.xlWorkbookDefaultExcel.XlFileFormat.xlOpenXMLWorkbook

 oWB.SaveAs(fileName, Excel.XlFileFormat.xlOpenXMLWorkbook, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

 oWB.SaveAs(fileName, Excel.XlFileFormat.xlWorkbookDefault, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

答案 1 :(得分:0)

保存为.xslx时,请尝试使用Excel.xlXMLSpreadsheet而不是Excel.XlFileFormat.xlWorkbookNormal