继有关生成 Excel 文件的问题后,我需要能够在 Webform 应用程序位于远程 Web 服务器上时在本地创建文件。这不是我以前处理过的事情,所以我发现很难确切地问什么。我在 VS2010 上使用 C# 的 WebForms。伊安德森 https://stackoverflow.com/users/408182/eandersson给我指明了方向简单Excel https://github.com/mstum/Simplexcel by 迈克尔·斯图姆 https://stackoverflow.com/users/91/michael-stum这似乎可以解决问题,但文件是在服务器上生成的(或者我应该说“尝试”,因为这是不允许的!!!)。
您应该能够执行与此类似的操作来生成并下载Excel Sheet
.
protected void generateExcelSheet_click(object sender, EventArgs e)
{
// Create Excel Sheet
var sheet = new Worksheet("Hello, world!");
sheet.Cells[0, 0] = "Hello,";
sheet.Cells["B1"] = "World!";
var workbook = new Workbook();
workbook.Add(sheet);
// Save
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=MyExcelSheet.xls");
workbook.Save(Response.OutputStream, CompressionLevel.Maximum);
Response.End();
}
In the designer.aspx
:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="FileDownload.Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="btnExcel" runat="server" Text="Download Excel Sheet" onclick="generateExcelSheet_click" />
</form>
</body>
</html>
我基于此代码this http://www.c-sharpcorner.com/uploadfile/afenster/how-to-download-a-file-in-asp-net/教程。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)