如何解决未将对象引用设置为对象错误的实例

时间:2014-12-18 10:12:28

标签: c# .net

我正在制作一个程序,将csv(常用选定值文件)文件转换为xls(microsoft excel文件)文件

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Excel = Microsoft.Office.Interop.Excel;


namespace ConversionToXLSFile
{
    public partial class Converter : System.Web.UI.Page
    {
        //Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
        Excel.Application xlApp;
        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);
        protected void Page_Load(object sender, EventArgs e)
        {
            //List<string> row = new List<string>();

            string fullPath = @"D:\Work\Sep-14\ConversionToXLSFile\ConversionToXLSFile\File\diff_16122014095440.csv";
            string[] fileRows = File.ReadAllLines(fullPath, Encoding.UTF8);

            foreach (string rows in fileRows)
            {
                var columns = rows.Split(';');

                for (int j = 0; j < fileRows.Length; j++)
                {
                    for (int i = 0; i < columns.Length; i++)
                    {
                        List<string> elements = new List<string>();
                        foreach (string col in columns)
                        {
                            elements.Add(col);
                            xlWorkSheet.Cells[j, i] = col;
                        }
                    }
                }
            }




            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();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
        }

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                Console.WriteLine("Exception Occured while releasing object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }
        private void Add(dynamic dynamic)
        {
            throw new NotImplementedException();
        }

        }
}

1 个答案:

答案 0 :(得分:2)

据我所知,您已经注释掉了分配变量xlApp,xlWorkBook和xlWorkSheet的代码...

//Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
//xlWorkBook = xlApp.Workbooks.Add(misValue);
//xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

从每行中取出//当你尝试使用它们时,你不会得到空引用异常。