Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WinForm应用程序通过模板生成的Excel文件有时为;无形的;_C#_.net_Winforms_Vsto_Office Automation - Fatal编程技术网

C# WinForm应用程序通过模板生成的Excel文件有时为;无形的;

C# WinForm应用程序通过模板生成的Excel文件有时为;无形的;,c#,.net,winforms,vsto,office-automation,C#,.net,Winforms,Vsto,Office Automation,我们有一个WinForm应用程序,它使用VSTO从模板生成Excel文件 偶尔(经常)会打开文件(因为您可以使用光标键以及公式和单元格更改),但它是不可见的。即使保存文件并将其打开备份,它仍然不可见 用户使用Excel2007,我们几乎所有(6-8)的用户都会遇到这个问题 我给他们的(临时)解决办法是选择“全部安排”。完成此操作后,工作表将弹出到视图中 我在网上看到了一些原因和修复方法。从在Excel 2003中的模板中使用图形到Excel 2007中没有的模板中使用图形 模板非常“简单”。它有

我们有一个WinForm应用程序,它使用VSTO从模板生成Excel文件

偶尔(经常)会打开文件(因为您可以使用光标键以及公式和单元格更改),但它是不可见的。即使保存文件并将其打开备份,它仍然不可见

用户使用Excel2007,我们几乎所有(6-8)的用户都会遇到这个问题

我给他们的(临时)解决办法是选择“全部安排”。完成此操作后,工作表将弹出到视图中

我在网上看到了一些原因和修复方法。从在Excel 2003中的模板中使用图形到Excel 2007中没有的模板中使用图形

模板非常“简单”。它有公式、字体和颜色,仅此而已

模板(和WinForm应用程序)通过单击一次部署给用户

以下是“ThisWorkbook.cs”文件中的代码:

以下是“Sheet1.cs”文件中的代码:

private const int StartingDataRow=4;
拉斯特罗私人酒店;
private int NextAvailableColumn=18;
数据集myDS=新数据集();
Dictionary xlsColumnData=新字典();
私有void Sheet1_启动(对象发送方,System.EventArgs e)
{
尝试
{
如果(Globals.ThisWorkbook.TemplateTableName==string.Empty)抛出新异常(“注册表中未设置TemplateTableName”);
如果(Globals.ThisWorkbook.TemplateSelectStatement==string.Empty)抛出新异常(“注册表中未设置TemplateSelectStatement”);
Application.screenUpdate=false;
如果(Globals.ThisWorkbook.TemplateTableName.Length>31)
this.Name=Globals.ThisWorkbook.TemplateTableName.Substring(0,31);
其他的
this.Name=Globals.ThisWorkbook.TemplateTableName;
LoadTableData();
LoadDataArrays();
BindDataToColumns();
应用公式();
应用程序格式();
此.Range[“B4”,缺少].Select();
Application.screenUpdate=true;
AppLog.WriteEvent(DateTime.Now、Environment.UserName、Environment.MachineName、Globals.thishworkbook.TemplateTableName、TraceEventType.Information,“创建客户列表”);
Globals.ThisWorkbook.RemoveCustomization();
}
捕获(例外情况除外)
{
AppLog.Show(例如消息,“Sheet1\u启动”,MessageBoxButtons.OK,MessageBoxIcon.Error,MessageBoxDefaultButton.Button1,TraceEventType.Error);
}
最后
{
Application.screenUpdate=true;
}
}
私有void LoadTableData()
{
尝试
{
Application.Cursor=XlMousePointer.xlWait;
string selectCommandText=Globals.ThisWorkbook.TemplateSelectStatement.Replace(“[Bind_Type]”),将“dbo.GetBindingCodeDescription([Bind_Type])替换为Binding_Description”);
SqlDataAdapter da=新的SqlDataAdapter(选择CommandText,Public\u No\u Holdings.Properties.Settings.Default.myConnectionString);
da.SelectCommand.CommandTimeout=60;
如果(da.SelectCommand.Connection.State!=ConnectionState.Closed)da.SelectCommand.Connection.Close();
da.Fill(此为myDS);
LastRow=(StartingDataRow+this.myDS.Tables[0].Rows.Count)-1;
}
捕获(例外情况除外)
{
Show(例如Message,“加载表”、MessageBoxButtons.OK、MessageBoxIcon.Error);
}
最后
{
Application.Cursor=XlMousePointer.xlDefault;
}
}
私有void LoadDataArrays()
{
System.Data.DataTable dt=this.myDS.Tables[0];
//将数据插入对象[,]
对象[,]行数据;
dt.Columns[“Imprint”].ColumnName=“Publisher”//为Imprint数据集列添加别名以填充“Publisher”xls列
对于(int-iCol=0;iColpublic string TemplateTableName;
public string TemplateSelectStatement;

private void ThisWorkbook_Startup(object sender, System.EventArgs e)
{
    Properties.Settings.Default.myConnectionString = Registry.GetValue("myConnectionString", Properties.Settings.Default.appConnectionString).ToString();
    TemplateTableName = Registry.GetValue("TemplateTableName", string.Empty).ToString();
    TemplateSelectStatement = Registry.GetValue("TemplateSelectStatement", string.Empty).ToString();

    AppLog.ConnectionString = Properties.Settings.Default.myConnectionString;
}
private const int StartingDataRow = 4;

private int LastRow;
private int NextAvailableColumn = 18;

DataSet myDS = new DataSet();

Dictionary<string, object[,]> xlsColumnData = new Dictionary<string, object[,]>();


private void Sheet1_Startup(object sender, System.EventArgs e)
{
    try
    {
        if (Globals.ThisWorkbook.TemplateTableName == string.Empty) throw new Exception("TemplateTableName is not set in the registry.");
        if (Globals.ThisWorkbook.TemplateSelectStatement == string.Empty) throw new Exception("TemplateSelectStatement is not set in the registry.");

        Application.ScreenUpdating = false;

        if (Globals.ThisWorkbook.TemplateTableName.Length > 31)
            this.Name = Globals.ThisWorkbook.TemplateTableName.Substring(0, 31);
        else
            this.Name = Globals.ThisWorkbook.TemplateTableName;


        LoadTableData();
        LoadDataArrays();
        BindDataToColumns();
        ApplyFormulas();
        ApplyFormatting();

        this.Range["B4", missing].Select();

        Application.ScreenUpdating = true;

        AppLog.WriteEvent(DateTime.Now, Environment.UserName, Environment.MachineName, Globals.ThisWorkbook.TemplateTableName, TraceEventType.Information, "Creating customer list");

        Globals.ThisWorkbook.RemoveCustomization();
    }
    catch (Exception ex)
    {
        AppLog.Show(ex.Message, "Sheet1_Startup", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, TraceEventType.Error);
    }
    finally
    {
        Application.ScreenUpdating = true;
    }
}

private void LoadTableData()
{
    try
    {
        Application.Cursor = XlMousePointer.xlWait;

        string selectCommandText = Globals.ThisWorkbook.TemplateSelectStatement.Replace("[Bind_Type]", "dbo.GetBindingCodeDescription([Bind_Type]) AS Binding_Description");

        SqlDataAdapter da = new SqlDataAdapter(selectCommandText, Public_No_Holdings.Properties.Settings.Default.myConnectionString);
        da.SelectCommand.CommandTimeout = 60;

        if (da.SelectCommand.Connection.State != ConnectionState.Closed) da.SelectCommand.Connection.Close();
        da.Fill(this.myDS);

        LastRow = (StartingDataRow + this.myDS.Tables[0].Rows.Count) - 1;
    }
    catch (Exception ex)
    {
        AppLog.Show(ex.Message, "Loading Table", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
        Application.Cursor = XlMousePointer.xlDefault;
    }
}
private void LoadDataArrays()
{
    System.Data.DataTable dt = this.myDS.Tables[0];

    // insert the data into the object[,]
    object[,] rowData;
    dt.Columns["Imprint"].ColumnName = "Publisher"; //Alias the Imprint dataset column to populate the "Publisher" xls column
    for (int iCol = 0; iCol < dt.Columns.Count; iCol++)
    {
        rowData = new object[dt.Rows.Count, 1];

        for (int iRow = 0; iRow < dt.Rows.Count; iRow++)
        {                   
            switch (dt.Columns[iCol].ColumnName)
            {
                case "EAN":
                    rowData[iRow, 0] = "'" + dt.Rows[iRow][iCol];
                    break;

                case "IPage_Link":
                    rowData[iRow, 0] = String.Format("=HYPERLINK(\"{0}\", \"{1}\")", dt.Rows[iRow][iCol], "iPage");
                    break;

                default:
                    rowData[iRow, 0] = dt.Rows[iRow][iCol];
                    break;
            }
        }

        xlsColumnData.Add(dt.Columns[iCol].ColumnName, rowData);
    }
}

private void BindDataToColumns()
{
    NamedRange nr;
    Range rng;

    foreach (KeyValuePair<string, object[,]> kvp in xlsColumnData)
    {
        try
        {
            if (this.Controls.Contains(kvp.Key))
            {
                nr = (NamedRange)this.Controls[kvp.Key];

                // Reduce range (remove header rows)
                rng = this.Range[this.Cells[StartingDataRow, nr.Column], this.Cells[LastRow, nr.Column]];
                rng.Value2 = kvp.Value;
            }
            else
            {
                this.Cells[StartingDataRow - 1, NextAvailableColumn].Value2 = kvp.Key;
                rng = this.Range[this.Cells[StartingDataRow, NextAvailableColumn], this.Cells[LastRow, NextAvailableColumn]];
                rng.Value2 = kvp.Value;
                NextAvailableColumn++;
            }
        }
        catch (Exception ex)
        {
            AppLog.Show(ex.Message, "BindDataToColumns - " + kvp.Key, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

}

private void ApplyFormulas()
{
    Range rng;
    int iCol;

    // Book Invoice
    iCol = this.BookInvoice.Column;
    rng = this.Range[this.Cells[StartingDataRow, iCol], this.Cells[LastRow, iCol]];
    rng.Select();
    rng.FillDown();

    // Your Cost
    iCol = this.YourCost.Column;
    rng = this.Range[this.Cells[StartingDataRow, iCol], this.Cells[LastRow, iCol]];
    rng.Select();
    rng.FillDown();
}

private void ApplyFormatting()
{
    // For some reason Hyperlink columns get reset
    this.IPage_Link.Font.Name = this.EAN.Font.Name;
    this.IPage_Link.Font.Size = this.EAN.Font.Size;

    ((Range)this.Cells[StartingDataRow, 1]).EntireRow.Select();
    Application.ActiveWindow.FreezePanes = true;
}