Response.AddHeader("Content-Disposition", "attachment; filename=" +HttpUtility.UrlEncode( "UKEY申请统计.xls"));即加入HttpUtility.UrlEncode函数转换下.
// HttpUtility.HtmlEncode :将字符串转换为 HTML 编码的字符串。
/// <summary>
/// 针对施工单位按年或月导出其所申请的建筑物统计数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnExportExcel_Click(object sender, EventArgs e) {
if (string.IsNullOrEmpty(this.startDate.Value)) {
Response.Write("<script>alert('请输入查询日期!');window.location.href='UKeyStatistic.aspx'</script>");
return;
}
DateTime dateStart = DateTime.Parse(DateTime.Now.Year + "-" + DateTime.Now.AddMonths(-1).Month + "-1");
DateTime dateEnd = DateTime.Parse(DateTime.Now.Year + "-" + DateTime.Now.Month + "-1");
if (this.timeGranularity.Value == "年")//年
{
dateStart = DateTime.Parse(this.startDate.Value + "-1-1");
dateEnd = dateStart.AddYears(1);
}
if (this.timeGranularity.Value == "月")//年
{
dateStart = DateTime.Parse(this.startDate.Value);
dateEnd = dateStart.AddMonths(1);
}
string userStyle = Session[BaseLib.USER_UserStyle].ToString();
string cityCode = Session[BaseLib.USER_CityCode].ToString();
string manufacturerName = Session[BaseLib.USER_ApplyUnit].ToString();
if (string.IsNullOrEmpty(manufacturerName)) {
Response.Write("<script>alert('请联系管理员修改下公司简称!');window.location.href='UKeyStatistic.aspx'</script>");
return;
}
int rowCount = 0;
DataTable nodeDT = NodeInfoManager.GetBuildingsByManufacturerName(userStyle,cityCode,manufacturerName, dateStart, dateEnd,int.MaxValue,1,ref rowCount);
if (nodeDT == null || nodeDT.Rows == null || nodeDT.Rows.Count == 0) {
Response.Write("<script>alert('无记录,不能导出!');window.location.href='UKeyStatistic.aspx'</script>");
return;
}
var workbook = new NPOI.HSSF.UserModel.HSSFWorkbook();
//Create new Excel sheet
var sheet = workbook.CreateSheet("UKEY申请统计表");
//标题
NPOI.SS.UserModel.ICell cellTitle = sheet.CreateRow(0).CreateCell(0);
cellTitle.SetCellValue("UKEY申请统计表");
//设置标题行样式
NPOI.SS.UserModel.ICellStyle style = workbook.CreateCellStyle();
style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;
NPOI.SS.UserModel.IFont font = workbook.CreateFont();
font.FontHeight = 20 * 20;
style.SetFont(font);
cellTitle.CellStyle = style;
//合并标题行
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 4));
//(Optional) set the width of the columns
sheet.SetColumnWidth(0, 40 * 256);
sheet.SetColumnWidth(1, 15 * 256);
sheet.SetColumnWidth(2, 12 * 256);
sheet.SetColumnWidth(3, 20 * 256);
//第一行
var firstRow = sheet.CreateRow(1);
firstRow.CreateCell(0).SetCellValue("申请单位(加公章):");
firstRow.CreateCell(3).SetCellValue("申请时间:");
//第二行
var secondRow = sheet.CreateRow(2);
secondRow.CreateCell(0).SetCellValue("联系人:");
secondRow.CreateCell(3).SetCellValue("联系电话:");
//主体样式
NPOI.SS.UserModel.ICellStyle styleBody = workbook.CreateCellStyle();
styleBody.Alignment = NPOI.SS.UserModel.HorizontalAlignment.LEFT;
NPOI.SS.UserModel.IFont fontBody = workbook.CreateFont();
fontBody.FontHeight = 18 * 18;
styleBody.SetFont(fontBody);
firstRow.GetCell(0).CellStyle = styleBody;
firstRow.GetCell(3).CellStyle = styleBody;
secondRow.GetCell(0).CellStyle = styleBody;
secondRow.GetCell(3).CellStyle = styleBody;
//Create a header row
var headerRow = sheet.CreateRow(3);
//Set the column names in the header row
headerRow.CreateCell(0).SetCellValue("建筑物名称");
headerRow.CreateCell(1).SetCellValue("建筑物编号");
headerRow.CreateCell(2).SetCellValue("所属地市");
headerRow.CreateCell(3).SetCellValue("申请时间");
//(Optional) freeze the header row so it is not scrolled
sheet.CreateFreezePane(0, 1, 0, 1);
int rowNumber = 4;
string buildingName = "";
string buildingCode = "";
string cityName = "";
string createTime = "";
//Populate the sheet with values from the grid data
for (int i = 0; i < nodeDT.Rows.Count; i++) {
//Create a new row
var row = sheet.CreateRow(rowNumber++);
buildingName = nodeDT.Rows[i]["BuildingName"].ToString();
buildingCode = nodeDT.Rows[i]["BuildingCode"].ToString();
cityName = nodeDT.Rows[i]["CityName"].ToString();
createTime = nodeDT.Rows[i]["CreateTime"].ToString();
//Set values for the cells
row.CreateCell(0).SetCellValue(buildingName);
row.CreateCell(1).SetCellValue(buildingCode);
row.CreateCell(2).SetCellValue(cityName);
row.CreateCell(3).SetCellValue(createTime);
}
//Write the workbook to a memory stream
MemoryStream output = new MemoryStream();
workbook.Write(output);
Response.ContentType = "application/vnd.ms-excel";
if (Request.Browser.Browser != "IE") {
Response.AddHeader("Content-Disposition", "attachment; filename=" +
"UKEY申请统计表.xls");
}
else {
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(
"UKEY申请统计表.xls"));
}
Response.BinaryWrite(output.ToArray());
Response.End();
}