使用C#创建的Excel文件为空白,显示为灰色

问题描述 投票:0回答:1

我有下面的代码创建了一个excel文件。它在给定的位置完美地创建了该文件。但是,当我打开该文件时,该文件显示为灰色。它不会显示任何工作表。

       private void button1_Click(object sender, EventArgs e)
    {
        Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

        if (xlApp == null)
        {
            MessageBox.Show("Excel is not properly installed!!");
            return;
        }


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

        xlWorkBook = xlApp.Workbooks.Add(misValue);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        xlWorkSheet.Cells[1, 1] = "ID";
        xlWorkSheet.Cells[1, 2] = "Name";
        xlWorkSheet.Cells[2, 1] = "1";
        xlWorkSheet.Cells[2, 2] = "One";
        xlWorkSheet.Cells[3, 1] = "2";
        xlWorkSheet.Cells[3, 2] = "Two";



        xlWorkBook.SaveAs("d:\\csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();

        Marshal.ReleaseComObject(xlWorkSheet);
        Marshal.ReleaseComObject(xlWorkBook);
        Marshal.ReleaseComObject(xlApp);

下图

enter image description here

c# excel office-interop
1个回答
0
投票

遵循您的格式,这可能使您更接近解决方案。

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace WindowsFormsApp1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application application = null;
            Excel.Workbooks workbooks = null;
            Excel.Workbook workbook = null;
            Excel.Worksheet worksheet = null;

            application = new Excel.Application
            {
                Visible = true,
                ScreenUpdating = true,
                EnableEvents = true,
                DisplayAlerts = true,
            };

            if (application == null)
            {
                MessageBox.Show("Excel is not properly installed!!");
                return;
            }

            workbooks = application.Workbooks;
            application.Workbooks.Add();
            workbook = workbooks[1];
            worksheet = workbook.Worksheets[1];

            worksheet.Cells[1, 1] = "ID";
            worksheet.Cells[1, 2] = "Name";
            worksheet.Cells[2, 1] = "1";
            worksheet.Cells[2, 2] = "One";
            worksheet.Cells[3, 1] = "2";
            worksheet.Cells[3, 2] = "Two";

            workbook.SaveAs("d:\\csharp-Excel.xls", FileFormat: Excel.XlFileFormat.xlWorkbookNormal, AccessMode: Excel.XlSaveAsAccessMode.xlExclusive);
            workbook.Close(true);
            application.Quit();

            Marshal.ReleaseComObject(worksheet);
            Marshal.ReleaseComObject(workbook);
            Marshal.ReleaseComObject(application);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.