ASP.NET 在 Excel 中输出费用报告

发布于 2024-07-11 17:17:45 字数 1436 浏览 7 评论 0原文

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

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

发布评论

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

评论(7

扮仙女 2024-07-18 17:17:46

我很确定你可以使用 OleDB 打开 Excel 文件(带有模板)

示例 1

string ConnectionString=@"Provider=Microsoft.Jet.OLEDB.4.0;
                    Data Source=C:\Test Projects\Excel example\Excel - reading an excel file\ReadThis.xls;
                    Extended Properties=Excel 5.0";

//Create the connection
 System.Data.OleDb.OleDbConnection ExcelConnection = new System.Data.OleDb.OleDbConnection(ConnectionString);

链接到文章 1

示例 2

string filename = @"C:\testdata.xls";

string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +

"Data Source=" + filename + ";" +

"Extended Properties=Excel 8.0;";

OleDbConnection objConn = new OleDbConnection(connectionString);

objConn.Open();

OleDbCommand ObjCommand = new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);

OleDbDataAdapter objAdp = new OleDbDataAdapter();

objAdp.SelectCommand = ObjCommand;

DataSet myDataSet = new DataSet();

objAdp.Fill(myDataSet);

DataTable dataTable = myDataSet.Tables["Sheet1$"];

链接到第 2 条

打开连接后,您应该能够按照您喜欢的方式读取和写入!

I am pretty sure you can can use OleDB to open the excel file(with the template)

Example 1

string ConnectionString=@"Provider=Microsoft.Jet.OLEDB.4.0;
                    Data Source=C:\Test Projects\Excel example\Excel - reading an excel file\ReadThis.xls;
                    Extended Properties=Excel 5.0";

//Create the connection
 System.Data.OleDb.OleDbConnection ExcelConnection = new System.Data.OleDb.OleDbConnection(ConnectionString);

Link to article 1

Example 2

string filename = @"C:\testdata.xls";

string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +

"Data Source=" + filename + ";" +

"Extended Properties=Excel 8.0;";

OleDbConnection objConn = new OleDbConnection(connectionString);

objConn.Open();

OleDbCommand ObjCommand = new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);

OleDbDataAdapter objAdp = new OleDbDataAdapter();

objAdp.SelectCommand = ObjCommand;

DataSet myDataSet = new DataSet();

objAdp.Fill(myDataSet);

DataTable dataTable = myDataSet.Tables["Sheet1$"];

Link to Article 2

Once you have opened the connection you should be able to read and write to it however you like!

水溶 2024-07-18 17:17:46

您可以尝试 Spire.XLS ( http://www.e-iceblue.com/xls /xlsintro.htm

它不是免费的,但可以很好地读取和创建 XLS 文档,而无需在服务器上安装 Excel。

You can try Spire.XLS ( http://www.e-iceblue.com/xls/xlsintro.htm )

It's not free, but does a great job of reading and creating XLS documents without having to have Excel present on the server.

旧人 2024-07-18 17:17:46

我完成此操作的方法之一是使用 Office XML 生成文档。 由于文档需要采用特定的格式,所以事情可能会有点困难,但我过去所做的事情是将需要在报表上显示的数据导出到 XML 文件(保存在磁盘或内存中),然后应用 XSL 转换将内容转换为 Excel 可以加载的格式正确的文档。 作为参考,以下是一些供您参考的链接:

One of the ways that I have done this is using Office XML to generate the documents. Since you have a specific format that the document needs to be in, things might be a bit harder, but the way I have done things before in the past is to export the data that needs to be displayed on the report to an XML file (either saved on disk or in the memory) and then apply an XSL transform to convert things over to the correctly formatted document that Excel can load. For reference, here are some links for you:

电影里的梦 2024-07-18 17:17:46

尝试Aspose。 Cells - 它非常适合这个。

Try Aspose.Cells - it works great for this.

囍笑 2024-07-18 17:17:46

Telerik RAD Grid for Asp.NetAJAX 具有很好的 Excel 导出功能。

Telerik RAD Grid for Asp.NetAJAX has nice Excel export.

长亭外,古道边 2024-07-18 17:17:46

另外两个解决方案是:

  • 使用 OpenOffice(它们有一种不加载/显示 UI 的模式)
  • 使用 FileHelpers库(您可以读取/写入数据到 Excel 文件)

我必须说我对它们都没有经验...但它们听起来像是 JET.OLEDB 提供程序和通过 Xsl 的 Xml-to-Excel 之外的可靠替代品克里斯和罗布提到过(我过去都用过)。

只是想帮助您并提及一些替代方案(我不期望太多:))

Another two solutions are:

  • use OpenOffice (they have a mode where no UI is loaded/shown)
  • use the FileHelpers library (you can read/write data to an excel file)

I must say I don't have experience with either of them... but they sound like solid alternatives beside the JET.OLEDB provider and the Xml-to-Excel through Xsl (which I both have used in the past) mentioned by Chris and Rob.

Just wanted to help you on your way and mention some alternatives (I don't expect much points :) )

稚气少女 2024-07-18 17:17:46

虽然这不会处理您现有的电子表格,但生成 Excel 文件的一种非常简单且技术含量较低的方法是创建具有正确内容类型的 html 文件并使用 .xls 扩展名。 当 Excel(在客户端上)打开该文件时,它会被视为本机电子表格。

示例:

<html>
    <head>
        <meta http-equiv="Content-Type" content="application/vnd.ms-excel" />
        <title></title>
    </head>
    <body>
        <table>
            <tr>
                <td><b>Forename</b></td>
                <td><b>Gender</b></td>
            </tr>
            <tr>
                <td>Simon</td>
                <td>Male</td>
            </tr>
            <tr>
                <td>Katy</td>
                <td>Female</td>
            </tr>
        </table>
    </body>
</html>

警告:当然,您不会在真实文件中包含所有缩进和空格! 我正在使用 Excel 2003。

该技术的另一个小好处是这些文件的生成效率非常高。

缺点:我还没有找到将 html 转换为多个工作表的方法,或者做任何特别技术性的事情。

Although this won't process your existing spreadsheets, a really simple and low-tech way to generate Excel files is to create a html file with the correct content-type and use the .xls extension. When Excel (on the client) opens the file, it is treated as if it was a native spreadsheet.

Example:

<html>
    <head>
        <meta http-equiv="Content-Type" content="application/vnd.ms-excel" />
        <title></title>
    </head>
    <body>
        <table>
            <tr>
                <td><b>Forename</b></td>
                <td><b>Gender</b></td>
            </tr>
            <tr>
                <td>Simon</td>
                <td>Male</td>
            </tr>
            <tr>
                <td>Katy</td>
                <td>Female</td>
            </tr>
        </table>
    </body>
</html>

Caveats: Of course you would not include all the indentation and spacing in the real file! I am using Excel 2003.

Another small bonus of this technique is that these files are very efficient to generate.

Downsides: I've not found a way to turn html into multiple Worksheets, or to do anything particularly technical.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文