0

First of all in my code im opening an excel sheet and reading it into a list. Second of all im closing this excel sheet and creating a new excel sheet:

excel_init("C:\\Users\\oma\\Desktop\\excel2.xlsx"); // this path does NOT exist yet

Next its going to this method:

static void excel_init(String path)
{
    appExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();

    if (System.IO.File.Exists(path))
    {
        // then go and load this into excel
        newWorkbook_First = appExcel.Workbooks.Open(path, true, true);
        objsheet = (_Worksheet)appExcel.ActiveWorkbook.ActiveSheet;
    }
    else
    {
        try
        {
            appExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
            appExcel.Visible = true;
            newWorkbook_First = appExcel.Workbooks.Add(1);
            objsheet = (Microsoft.Office.Interop.Excel.Worksheet)newWorkbook_First.Sheets[1];
            objsheet.Name = ("test");
            var newSheet3 = (Microsoft.Office.Interop.Excel.Worksheet)appExcel.Worksheets.Add(Type.Missing, appExcel.Worksheets[appExcel.Worksheets.Count], 1, XlSheetType.xlWorksheet);
            newSheet3.Name = "test2";
            var newSheet2 = (Microsoft.Office.Interop.Excel.Worksheet)appExcel.Worksheets.Add(Type.Missing, appExcel.Worksheets[appExcel.Worksheets.Count], 1, XlSheetType.xlWorksheet);
            newSheet2.Name = "test3";
        }
        catch (Exception e)
        {
            Console.Write("Error");
        }
        finally
        {
        }
    }
}

It wont create excel2.xlsx on my desktop, why not?

Next in my main I am shouting excel_setValue this way:

excel_setValue("C", "hello", "");

This is the excel_setValue function:

static void excel_setValue(string cellname, string value, string color)
{
    objsheet.get_Range(cellname).set_Value(Type.Missing, value);
    if (kleur == "red")
    {
        objsheet.get_Range(cellname).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
    }
}

How do I add the sheet to this? For exmaple: excel_setValue(cellname, value, color, sheetname)

Finally I do excel_close();

the excel_close function:

static void excel_close()
        {
            if (appExcel != null)
            {
                try
                {
                    newWorkbook_First.Close();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel.ActiveWorkbook.ActiveSheet);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel.ActiveWorkbook);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel);
                    appExcel = null;
                    objsheet = null;
                }
                catch (Exception ex)
                {
                    appExcel = null;
                    Console.WriteLine("Unable to release the Object " + ex.ToString());
                }
                finally
                {
                    GC.Collect();
                }
            }
        }

It closes and ask me if I wanna save/not-save or cancel it. And it will save @ documents with no errors given

2
  • "It wont create excel2.xlsx on my desktop, why not?" - In what way does it fail? Is there an error? Is it creating the file somewhere else? Please include more details. Commented Jun 12, 2015 at 13:24
  • no errors were given, it closes and tells me if I want to save the file in documents, not save the file or just cancel Commented Jun 12, 2015 at 13:40

2 Answers 2

1

I create my Excel like this worked fine for me!

You need to do the folowing in you code before the newWorkbook_First.close(); part:

 newWorkbook_First.SaveAs(totalPath);

I hope this will help you solve the problems..

using Excel = Microsoft.Office.Interop.Excel;


internal static bool ExportDGV(DataGridView DGV, List<string> selectedCustomerList, string path, string fileName, string exportName, int exportType, string mailSubject)
{
    string totalPath;
    //Create an Excel application instance
    Excel.Application excelApp = new Excel.Application();
    Excel.Workbook excelWorkBook = excelApp.Application.Workbooks.Add();
    Excel._Worksheet worksheet = null;
    excelApp.Visible = false;
    worksheet = excelWorkBook.ActiveSheet;

    //set headers
    for (int i = 1; i < DataGridView.Columns.Count + 1; i++)
    {
    worksheet.Cells[1, i] = DGV.Columns[i - 1].HeaderText;
    }
    createList(worksheet, DGV);

    //Create excel with the choosen name
    Worksheet sheet1 = excelWorkBook.Worksheets[1];
    worksheet.Name = fileName;

    totalPath = path + "/" + fileName + ".xlsx";

    //if path exist add a number
    totalPath = directoryExist(totalPath, path, fileName);
    //Save exel and Quit exelApp 

    excelWorkBook.SaveAs(totalPath);
    excelWorkBook.Close();
    excelApp.Quit();
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can use EpPlus library for excel. It is very easy to use and well documented. You don't need to use COM classes anymore

1 Comment

This should be a comment on the question, rather than an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.