none
Install Interop.Microsoft.Office.Interop.Excel.dll on GAC .net 4.0 windows server 2008 RRS feed

  • Question

  • HI I need to add the Interop.Microsoft.Office.Interop.Excel.dll in to the gac, this is because I create a Excl file on my ASP page for download, but I can't found the GAC for .net 4.0 I try to use the gac on C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools

    and C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\

    and not works I have this issue:

    Microsoft (R) .NET Global Assembly Cache Utility.  Version 3.5.30729.1
    Copyright (c) Microsoft Corporation.  All rights reserved.

    Failure adding assembly to the cache: Attempt to install an assembly without a strong name

    I  try with the steps on http://sujeetji.wordpress.com/2011/01/09/assign-change-strong-name-to-existing-assembly/ but the I have this issue:

    'ilasm' is not recognized as an internal or external command, operable program or batch file.

    when I search 'ilasm'  on the server I found on:

    C:\Windows\Microsoft.NET\Framework64\v4.0.30319

    I run but I continue with the same issue, please let me know if you know other way,

    my server is Windows 2008 and I can't install office or Visual studio on my server, please let me know if you know how I can' make this.

    Best Regards.

    Sunday, May 19, 2013 4:22 PM

Answers

  • See if the code below works.  I had trouble with it on a machine that didn't have excel installed but it may work for you . It uses the oledb class with Extended properties for Excel.  I thought this would work on a machine that did't have excel installed, but now I'm not sure since the problem I had this week.  Let me know if it works then I will know if my problem was solely due to excel not being installed.

            class Workbook : IDisposable
            {
                OleDbConnection conn;
                OleDbCommand cmd;
                string connectionStr;
                const int MAX_COLUMN_WIDTH = 255;
                List<ColumnHeader> columnDefinitions = new List<ColumnHeader>();
                long rowCount = 0;
                int sheetCount = 0;
                public Boolean CreateWorkbook(string filename)
                {
                    Boolean succesful = true;
                    FileInfo file = new FileInfo(filename);
                    DialogResult answer = new DialogResult();
                    answer = DialogResult.Yes;
                    if (file.Exists == true)
                    {
                        // If the workbooks already exist, prompt to delete.
                        answer = MessageBox.Show(
                           "Delete existing workbooks (" + filename + ")?",
                           "Workbook Exists",
                        MessageBoxButtons.YesNo);
                        if (answer == DialogResult.Yes)
                        {
                            try
                            {
                                file.Delete();
                                Application.DoEvents();
                            }
                            catch
                            {
                                MessageBox.Show("Cannot Delete file");
                                succesful = false;
                            }
                        }
                    }
                    if (succesful)
                    {
                                        
                        connectionStr = string.Format(
                           "Provider=Microsoft.Jet.OLEDB.4.0;" +
                           @"Data Source={0};" +
                           "Extended Properties=\"Excel 8.0;HDR=YES\"",
                           filename);
                        try
                        {
                            conn = new OleDbConnection();
                            conn.ConnectionString = connectionStr;
                            conn.Open();
                            cmd = new OleDbCommand();
                            cmd.Connection = conn;
                            Application.DoEvents();
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show("Cannot create Excel workbook");
                            succesful = false;
                        }
                    }
                    return succesful;
                }
                public Boolean CreateHeaders(DataTable table)
                {
                    Boolean succesful = true;
                    string columnFormatText = "";
                    string newcolFormat = "";
                    foreach (DataColumn col in table.Columns)
                    {
                        switch (col.DataType.Name)
                        {
                            case "Byte":
                                newcolFormat = "'" + col.ColumnName + "' byte";
                                break;
                            case "Int16":
                                newcolFormat = "'" + col.ColumnName + "' integer";
                                break;
                            case "Int32":
                                newcolFormat = "'" + col.ColumnName + "' integer";
                                break;
                            case "Int64":
                                newcolFormat = "'" + col.ColumnName + "' long";
                                break;
                            case "Double":
                                newcolFormat = "'" + col.ColumnName + "' double";
                                break;
                            case "Guid":
                            case "String":
                                newcolFormat = "'" + col.ColumnName + "' char(" + MAX_COLUMN_WIDTH.ToString() + ")";
                                break;
                            case "DateTime":
                                newcolFormat = "'" + col.ColumnName + "' date";
                                break;
                            default:
                                break;
                        }
                        if (columnFormatText.Length == 0)
                        {
                            columnFormatText = newcolFormat;
                        }
                        else
                        {
                            columnFormatText += "," + newcolFormat;
                        }
                        ColumnHeader newColHeader = new ColumnHeader();
                        newColHeader.name = col.ColumnName;
                        newColHeader.format = newcolFormat;
                        columnDefinitions.Add(newColHeader);
                    } //end foreach
                    try
                    {
                        sheetCount += 1;
                        rowCount = 1;
                        cmd.CommandText = string.Format("CREATE TABLE Results_{0} ({1})", 
                            sheetCount.ToString(), columnFormatText);
                        cmd.ExecuteNonQuery();
                        rowCount += 1;
                        Application.DoEvents();
                       
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("Cannot add columns to Excel workbook ({0})", e.Message);
                        succesful = false;
                    }
                
                    return succesful;
                }
                public Boolean WriteData(DataTable table)
                {   
                    Boolean succesful = true;
                    string columnValuesText = "";
                    string columnValues = "";
                    int rownumber = 0;
                    string columnsHeaderText;
                    foreach (DataRow row in table.Rows)
                    {
                        try
                        {
                            int colIndex = 0;
                            columnValuesText = "";
                            columnsHeaderText = "";
                            foreach (var colText in row.ItemArray)
                            {
                                if (!(colText is DBNull))
                                {
                                    if (columnsHeaderText.Length == 0)
                                    {
                                        columnsHeaderText = "'" + columnDefinitions[colIndex].name + "'";
                                    }
                                    else
                                    {
                                        columnsHeaderText += "," + "'" + columnDefinitions[colIndex].name + "'";
                                    }
                                    if (colText.GetType().ToString() == "System.String")
                                    {
                                        columnValues = (string)colText;
                                        if (columnValues.Length > (MAX_COLUMN_WIDTH - 2))
                                        {
                                            columnValues = columnValues.Substring(0, MAX_COLUMN_WIDTH - 2);
                                        }
                                        //Mar 13, 2013 changed from: columnValues to: columnValuesText 
                                        if (columnValuesText.Length == 0)
                                        {
                                            columnValuesText = "'" + columnValues + "'";
                                        }
                                        else
                                        {
                                            columnValuesText += ",'" + columnValues + "'";
                                        }
                                    }
                                    else
                                    {
                                        if (columnValuesText.Length == 0)
                                        {
                                            columnValuesText = "'" + colText + "'";
                                        }
                                        else
                                        {
                                            columnValuesText += ",'" + colText + "'";
                                        }
                                    }
                                }
                                colIndex += 1;
                            }
                            if (rowCount > 65536)
                            {
                                CreateHeaders(table);
                            }
                            cmd.CommandText = string.Format("INSERT INTO Results_{0} ({1}) values ({2})",
                                sheetCount.ToString(), columnsHeaderText, columnValuesText);
                            rowCount += 1;
                            cmd.ExecuteNonQuery();
                        }
                        catch (Exception e)
                        {
                            string errormsg = string.Format("Cannot cannot add row to Excel workbook {0}",
                                e.Message);
                            MessageBox.Show("Cannot cannot add row to Excel workbook");
                            succesful = false;
                        }
                        rownumber++;
                    }
                    return succesful;
                }
                public void Dispose()
                {
                    conn.Close();
                    columnDefinitions = null;
                }
            }


    jdweng

    Sunday, May 19, 2013 5:53 PM