将 Excel 导出到 C#

发布于 2024-09-13 11:55:07 字数 585 浏览 5 评论 0 原文

我必须将业务人员用来计算最终价格的 excel 文件移植到 C#,这样我以后就可以在 Asp.Net 应用程序中使用这个算法。这不是我第一次做这件事,但今天它让我震惊。也许 Excel 可以导出到 C#。我不在乎它是否会看起来丑陋,但这仍然是一个开始。

是否有一些 Excel 插件,我可以在其中标记一些输入和输出单元格,并且此添加会将其导出到 C#?

编辑

我们的客户有 Excel 文件,员工可以在其中修改某些字段并在另一个字段中查看最终价格。 Excel文件没有任何VB,而是使用标准数学函数来计算价格。

想象一下用于计算活期保险价格的 Excel 文件。

  • Excel 文件有空单元格,公司雇主必须填写:客户年龄、不良习惯、保险价格、保险期限、折扣等
  • Excel 文件有许多带有常量的单元格。常量很可能是系数(例如保险合同签订年数的价格系数。年数越多,价格越少)
  • Excel 文件有许多带有中间值的单元格
  • Excel 文件有一个输出单元格,其中包含整个保险合同的最终保险价格 中。

我要做的就是将excel文件进​​行的计算移植到C#

I have to port excel file which business guys are using to calculate final price to C# so I can later use this algorithm in Asp.Net application. This is something I will be doing not for the first time, but today it hit me. Maybe Excel can export to C#. I don't care if it will look ugly, but this is still something to start with.

Is there some Excel plugin where I could mark some input and output cells and this add-it would export it to C#?

Edit

Our client have Excel file where employes can modify some fields and see final price in another field. Excel file doesn't have any VB, but uses standard mathematical functions functions to calculate the price.

Imagine excel file used to calculate live insurance price.

  • Excel file has empty cells company employer has to fill: client age, bad habits, insurance price, insurance period, discount and so on
  • Excel file has many cells with constants. Constants are most likely coeficient (like price coefficient for number of years insurance contract is signed for for. The more years the less is the price)
  • Excel file has many cells with intermediate values
  • Excel file have one output cell with final insurance price for the whole period

What I have to do is to port calculations made with excel file to C#.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

对你的占有欲 2024-09-20 11:55:07

严格来说,“将 Excel 导出到 C#”并没有多大意义。这是因为您通常希望从 Excel 导出的是数据,而 C# 是一种编程语言。也许您正在寻找的问题是“如何使 Excel 数据可供用 C# 编写的程序使用?”

我使用这篇关于 C# Excel Interop Use 的文章作为从应用程序读取 Excel 数据的起点用 C# 编写。这可能是开始了解如何使 Excel 数据在 C# 中可见的好地方。

解释您的问题的另一种方法是您希望将 Excel 工作簿中的 Visual Basic for Applications (VBA) 代码转换为 C#。如果是这种情况,您有几种选择:

  • 如果代码不是很多,那么将算法和过程从 VBA 一次一个地转换为 C# 可能是最简单的方法。
  • 这是一种相当丑陋的方法,但您可以将 VBA 代码导入 Visual Studio 并将其上转换为 VB.NET。然后您可以使用 reflector 将代码反射到 C#。当然,这假设 VBA 代码与各个 Excel 对象(工作表、行、单元格、范围等)没有紧密耦合;如果它与我见过(并编写过)的 Excel“业务应用程序”类似,则情况不太可能是这样。
  • 对于具有解耦代码的绝对巨大应用程序,您可能会考虑VB6到.NET的迁移解决方案,例如VB 迁移合作伙伴。如果您有紧密耦合的代码,我不确定这些解决方案的效果如何,但如果工作簿很大,可能值得一看。

Strictly speaking, it doesn't make a lot of sense to "export Excel to C#." This is because what you generally wish to export from Excel is data and C# is a programming language. Perhaps the question you're looking for is, "How can I make Excel data available to a program written in C#?"

I used this write-up on C# Excel Interop Use as a starting point for reading Excel data from an application written in C#. That might be a good place to start to figure out how to make Excel data visible in C#.

The other way to interpret your question is that you want to convert the Visual Basic for Applications (VBA) code within an Excel workbook to C#. If this is this case, you have a few options:

  • If it's not a lot of code, it probably would be easiest to simply convert the algorithms and procedures one at a time from VBA to C#.
  • This is a pretty ugly way, but you could import the VBA code into Visual Studio and upconvert it to VB.NET. Then you could use reflector to reflect the code to C#. Granted, this assumes that the VBA code is not tightly coupled to individual Excel objects (worksheets, rows, cells, ranges, etc.); if it's anything like the Excel "business applications" I've seen (and written), that's not likely to be the case.
  • For absolutely huge applications with decoupled code, you might consider VB6 to .NET migration solutions, like VB Migration Partner. I'm not sure how well these solutions will work if you have tightly coupled code, but if the workbook is huge, it might be something to look at.
苏大泽ㄣ 2024-09-20 11:55:07

如果您的意思是:

  • 我有一个 Excel VBA 应用程序,我希望移植到 C#

那么这个问题可能会有所帮助:

If you mean:

  • I have an Excel VBA application that I wish to port to C#

Then this question may be of some assistance:

御弟哥哥 2024-09-20 11:55:07

您无法真正从电子表格创建代码。您更需要一个读取 Excel 文件的 .NET API。其中有几个。这是我使用的:
http://exceldatareader.codeplex.com/

You can't really create code from a spead-sheet. You rather want a .NET API that reads excel files. Of which there are a few. This is the one I use:
http://exceldatareader.codeplex.com/

子栖 2024-09-20 11:55:07

我从未使用过它,但您可能想看看:

http://www.calc4web.com/< /a>

它似乎可以做你想做的事,只是用 C++ 而不是 C#,但重点是你得到一些代码,然后可以编译和使用(或者翻译,如果你真的需要它是 C#)。

在这个问题的答案中:

理解/建模 Excel 中的公式

Joe Erickson 提供了他的产品 Spreadsheet Gear 的链接,但我也从未使用过它。

I've never used it, but you might want to look at this:

http://www.calc4web.com/

It seems to do what you want, only with C++ instead of C#, but the point is that you get some code you can then compile and use (or translate if you really need it to be C#).

In the answers to this question:

Understanding / Modeling formulas from Excel

Joe Erickson gave a link to his product, Spreadsheet Gear, but I've never used that either.

傲鸠 2024-09-20 11:55:07

如果您的公司有 SharePoint 2007 或 2010,您可能需要查看 Excel Services,它允许您通过 SharePoint Web 部件在 Excel 工作簿中使用计算。不确定您是否能够将该 Web 部件公开给其他 ASP.NET 应用程序,但这可能值得研究

If your company has SharePoint 2007 or 2010, you may want to take a look at Excel Services, which lets you use calculations in Excel workbooks via a SharePoint web part. Not sure if you would be able to expose that web part to other ASP.NET applications or not, but it might be worth investigating

栩栩如生 2024-09-20 11:55:07

尝试使用此代码,可能会有所帮助

    public static void DataSetsToExcel(DataSet dataSet, string filepath)
    {
        try
        {
            string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=Excel 12.0 Xml;";
            string tablename = "";
            DataTable dt = new DataTable();
            foreach (System.Data.DataTable dataTable in dataSet.Tables)
            {
                dt = dataTable;
                tablename = dataTable.TableName;
                using (OleDbConnection con = new OleDbConnection(connString))
                {
                    con.Open();
                    StringBuilder strSQL = new StringBuilder();
                    strSQL.Append("CREATE TABLE ").Append("[" + tablename + "]");
                    strSQL.Append("(");
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        strSQL.Append("[" + dt.Columns[i].ColumnName + "] text,");
                    }
                    strSQL = strSQL.Remove(strSQL.Length - 1, 1);
                    strSQL.Append(")");

                    OleDbCommand cmd = new OleDbCommand(strSQL.ToString(), con);
                    cmd.ExecuteNonQuery();

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        strSQL.Clear();
                        StringBuilder strfield = new StringBuilder();
                        StringBuilder strvalue = new StringBuilder();
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
                            strfield.Append("[" + dt.Columns[j].ColumnName + "]");
                            strvalue.Append("'" + dt.Rows[i][j].ToString().Replace("'", "''") + "'");
                            if (j != dt.Columns.Count - 1)
                            {
                                strfield.Append(",");
                                strvalue.Append(",");
                            }
                            else
                            {
                            }
                        }
                        if (strvalue.ToString().Contains("<br/>"))
                        {
                            strvalue = strvalue.Replace("<br/>", Environment.NewLine);
                        }
                        cmd.CommandText = strSQL.Append(" insert into [" + tablename + "]( ")
                            .Append(strfield.ToString())
                            .Append(") values (").Append(strvalue).Append(")").ToString();
                        cmd.ExecuteNonQuery();
                    }
                    con.Close();
                }
            }
        }
        catch (Exception ex)
        {                
        }
    }

Try to use this code, may it ll help

    public static void DataSetsToExcel(DataSet dataSet, string filepath)
    {
        try
        {
            string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=Excel 12.0 Xml;";
            string tablename = "";
            DataTable dt = new DataTable();
            foreach (System.Data.DataTable dataTable in dataSet.Tables)
            {
                dt = dataTable;
                tablename = dataTable.TableName;
                using (OleDbConnection con = new OleDbConnection(connString))
                {
                    con.Open();
                    StringBuilder strSQL = new StringBuilder();
                    strSQL.Append("CREATE TABLE ").Append("[" + tablename + "]");
                    strSQL.Append("(");
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        strSQL.Append("[" + dt.Columns[i].ColumnName + "] text,");
                    }
                    strSQL = strSQL.Remove(strSQL.Length - 1, 1);
                    strSQL.Append(")");

                    OleDbCommand cmd = new OleDbCommand(strSQL.ToString(), con);
                    cmd.ExecuteNonQuery();

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        strSQL.Clear();
                        StringBuilder strfield = new StringBuilder();
                        StringBuilder strvalue = new StringBuilder();
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
                            strfield.Append("[" + dt.Columns[j].ColumnName + "]");
                            strvalue.Append("'" + dt.Rows[i][j].ToString().Replace("'", "''") + "'");
                            if (j != dt.Columns.Count - 1)
                            {
                                strfield.Append(",");
                                strvalue.Append(",");
                            }
                            else
                            {
                            }
                        }
                        if (strvalue.ToString().Contains("<br/>"))
                        {
                            strvalue = strvalue.Replace("<br/>", Environment.NewLine);
                        }
                        cmd.CommandText = strSQL.Append(" insert into [" + tablename + "]( ")
                            .Append(strfield.ToString())
                            .Append(") values (").Append(strvalue).Append(")").ToString();
                        cmd.ExecuteNonQuery();
                    }
                    con.Close();
                }
            }
        }
        catch (Exception ex)
        {                
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文