我有一些带有一些字段的工资表,我希望从asp.net中的excel格式中检索数据。
我使用此编码导出excel文件格式,但我没有得到任何excel文件和错误
所以请指导我做错了什么
int id=0;
if (ddlcategory.SelectedValue == "1")
{
id = 1;
}
else if (ddlcategory.SelectedValue == "2")
{
id = 2;
}
objinc.Empno = txtfempno.Text;
objinc.Machno = txttempno.Text;
objinc.Depart = ddlDepartment.SelectedItem.Text;
objinc.Emptype = id;
DataTable dt = new DataTable();
dt = objdata.sel_Increment(objinc);
gdvincrement.DataSource = dt;
gdvincrement.DataBind();
if (dt.Rows.Count > 0)
{
string attachment = "attachment;filename=Increment_exc.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new HtmlTextWriter(stw);
gdvincrement.RenderControl(htextw);
Response.Write(stw.ToString());
Response.End();
Response.Clear();
ScriptManager.RegisterStartupScript(this, this.GetType(), "window", "alert('Downloaded Successfully');", true);
答案 0 :(得分:1)
我知道最有用的方法是使用excel xml格式。它也与MVC模式(或者只是aspx页面)很好地结合在一起。因此,您应该首先做的是为您的xml文件准备视图模型(如果您使用的是MVC模式)。 (与通常为html视图执行此操作的方式相同)。然后你应该定义将是excel xml模式的视图(视图模型的视图)。在该xml模式中,您应该使用模型在其中传递数据。
示例:
Excel.cshtml
@model MySite.ViewModels.Users.ExcelViewModel
@{
this.Layout = null;
this.Response.ContentType = "application/vnd.ms-excel";
this.Response.AddHeader("Content-Disposition", "attachment; filename=users.xls");
}
<?xml version="1.0" encoding="utf-16"?>
<ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<ss:Styles>
<ss:Style ss:ID="sHeader">
<ss:Borders>
<ss:Border ss:LineStyle="Continuous" ss:Weight="1" ss:Position="Bottom" />
</ss:Borders>
<ss:Font ss:Bold="1"/>
<ss:Interior ss:Color="#F0F0F0" ss:Pattern="Solid"/>
</ss:Style>
</ss:Styles>
<ss:Worksheet ss:Name="Пользователи">
<ss:Table>
<ss:Column ss:AutoFitWidth="0" ss:Width="192"/>
<ss:Column ss:AutoFitWidth="0" ss:Width="192"/>
<ss:Row>
@foreach (var item in this.Model.grid.columns)
{
<ss:Cell ss:StyleID="sHeader">
<ss:Data ss:Type="String">@item.name</ss:Data>
</ss:Cell>
}
</ss:Row>
@foreach (var item in this.Model.items)
{
<ss:Row>
<ss:Cell>
<ss:Data ss:Type="String">@item.eMail</ss:Data>
</ss:Cell>
<ss:Cell>
<ss:Data ss:Type="String">@item.name</ss:Data>
</ss:Cell>
</ss:Row>
}
</ss:Table>
</ss:Worksheet>
</ss:Workbook>
然后只需创建通常的控制器操作并从中返回此视图。就是这样。
如果您不熟悉excel xml语法(像我一样),您可以使用excel准备文档,然后将其保存为xml格式并与您的应用程序一起使用。
如果您使用网页而不是mvc,您也可以使用类似的方法。
希望这有帮助!
答案 1 :(得分:0)
试试这个,
这只是一个例子:
public ActionResult ExportProductManagement(string DownloadToken)
{
var productList = _taskProduct.ExportProductManagement(); //= objTask.ExportTroubleCode();
var result = new StringBuilder();
result.Append("Part Number|Test Code|Exclude|Material Description|GoAxial Material Description|Division|Distribution Channel|Plant|Material Group 1|Material Group2|Material Group4|Material Group 5|List Price|GoTo Quantity Limit|GoTo Lead Time|Product Hierarchy|Status| DChain Status|ExtractDate \r\n");
foreach (var item in productList)
{
//var itemResult = item.PartNumber + ',' + string.Format("'{0}'", item.TestCode) + ',' + item.MaterialDescription + ',' + item.Division + ',' + item.Plant + ',' + item.MaterialGroup2 + ',' + item.MaterialGroup4 + ',' + item.ExtractDate + ',' + item.IsExcluded + "\r\n";
var itemResult = string.Format("{0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}|{8}|{9}|{10}|{11}|{12}|{13}|{14}|{15}|{16}|{17}|{18} \r\n", item.PartNumber, item.TestCode, item.IsExcluded, item.MaterialDescription, item.GoAxialParseMaterialDescription != null? item.GoAxialParseMaterialDescription.Trim('#'):null, item.Division, item.DistributionChannel, item.Plant, item.MaterialGroup1, item.MaterialGroup2, item.MaterialGroup4, item.MaterialGroup5, item.ListPrice, item.GoToQuantityLimit, item.GoToLeadTime, item.ProductHierarchy,item.Status ,item.DChainStatus, item.ExtractDate);
result.Append(itemResult);
}
var byteArray = Encoding.ASCII.GetBytes(result.ToString());
var stream = new MemoryStream(byteArray);
var textName = "Test_ExportFile_" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls";
//use the submitted download token value to set a cookie in the response
Response.AppendCookie(new HttpCookie("fileDownloadToken", DownloadToken));
return File(stream, "application/excel", textName);
}