在利用开源MyXls导出Excel时,对于安装着迅雷的客户端,导出时会关联迅雷,如果用迅雷下载,下载的是这个页面即aspx文件,而非想要的的报表
为了解决这个问题费尽心思,bd,Google,没找到答案,后来自己看看源码,细细研究了一下解决了
迅雷下载时重定位了资源,所以下载得到的是网页的。这样我们可以每次点击导出时转到另一个新的页面,在新页面的page_load中处理
导出,导出时用迅雷就会得到需要的报表而不是aspx文件。但是还有问题,就是用迅雷下载的报表可能打不开或出问题,这样又费了些力气,在
代码中加入了两行代码,才得以解决。即:先把要导出的报表在服务器保存一份,再send(),如下处理
string path = Server.MapPath("./");
xls.Save(path, true);
xls.Send();
System.IO.File.Delete(path + xls.FileName);
在转交新页面时,对于redirect直接转过去的,不会有问题,挺好,而对于window.open()打开的,如果在url参数中有中文,问题就又出
来了。迅雷下载会出毛病。经过一番实验,查找,得以解决。对于新页面用window.open()打开的,如果url参数中带有中文,则要传递时用escape(),
在新页面中接受时,要用Microsoft.JScript.GlobalObject.unescape(),这样,问题就都解决了。
下面是一导出代码示例:
private void ExcelExport(IList< LineLossInfo > lossListExcel)
{
XlsDocument xls = new XlsDocument();
xls.FileName = "LineLossMonthDetail.xls";//指定文件名
Worksheet sheet = xls.Workbook.Worksheets.Add("LineLossMonthDetail");
#region 设置各数据列的大小
ColumnInfo colInfo1 = new ColumnInfo(xls, sheet);
colInfo1.ColumnIndexStart = 0;
colInfo1.ColumnIndexEnd = 1;
colInfo1.Width = 15 * 256;
sheet.AddColumnInfo(colInfo1);
ColumnInfo colInfo2 = new ColumnInfo(xls, sheet);
colInfo2.ColumnIndexStart = 2;
colInfo2.ColumnIndexEnd = 2;
colInfo2.Width = 15 * 256;
sheet.AddColumnInfo(colInfo2);
ColumnInfo colInfo3 = new ColumnInfo(xls, sheet);
colInfo3.ColumnIndexStart = 3;
colInfo3.ColumnIndexEnd = 3;
colInfo3.Width = 15 * 256;
sheet.AddColumnInfo(colInfo3);
ColumnInfo colInfo4 = new ColumnInfo(xls, sheet);
colInfo4.ColumnIndexStart = 4;
colInfo4.ColumnIndexEnd = 4;
colInfo4.Width = 15 * 256;
sheet.AddColumnInfo(colInfo4);
ColumnInfo colInfo5 = new ColumnInfo(xls, sheet);
colInfo5.ColumnIndexStart = 5;
colInfo5.ColumnIndexEnd = 5;
colInfo5.Width = 32 * 256;
sheet.AddColumnInfo(colInfo5);
#endregion
Cells cells = sheet.Cells;
#region 合并单元格,得到报表标题
MergeArea maTitle = new MergeArea(1, 2, 1, 6);
sheet.AddMergeArea(maTitle);
XF xfTitle = xls.NewXF();
xfTitle.HorizontalAlignment = HorizontalAlignments.Centered;
xfTitle.VerticalAlignment = VerticalAlignments.Centered;
xfTitle.Font.FontName = "宋体";
xfTitle.Font.Height = 16 * 20;
xfTitle.Font.Bold = true;
cells.Add(1, 1, divDataHeader.InnerText, xfTitle);
#endregion
MergeArea maTime1 = new MergeArea(3, 3, 1, 6);
sheet.AddMergeArea(maTime1);
XF xfTopBar = xls.NewXF();
xfTopBar.Font.FontName = "宋体";
cells.Add(3, 1, divDataCondition.InnerText, xfTopBar);
#region 设置Excel数据列标题的格式
XF xfDataHead = xls.NewXF();
xfDataHead.HorizontalAlignment = HorizontalAlignments.Centered;
xfDataHead.VerticalAlignment = VerticalAlignments.Centered;
xfDataHead.Font.FontName = "宋体";
xfDataHead.Font.Bold = true;
xfDataHead.UseBorder = true;
xfDataHead.BottomLineStyle = 1;
xfDataHead.BottomLineColor = Colors.Black;
xfDataHead.TopLineStyle = 1;
xfDataHead.TopLineColor = Colors.Black;
xfDataHead.LeftLineStyle = 1;
xfDataHead.LeftLineColor = Colors.Black;
xfDataHead.RightLineStyle = 1;
xfDataHead.RightLineColor = Colors.Black;
#endregion
#region 添加列标题
cells.Add(4, 1, "日期", xfDataHead);
cells.Add(4, 2, "供电量", xfDataHead);
cells.Add(4, 3, "用电量", xfDataHead);
cells.Add(4, 4, "损失量", xfDataHead);
cells.Add(4, 5, "损失率", xfDataHead);
cells.Add(4, 6, "说明", xfDataHead);
#endregion
#region 设置各数据列的格式
XF xfData = xls.NewXF();
xfData.Font.FontName = "宋体";
xfData.UseBorder = true;
xfData.BottomLineStyle = 1;
xfData.BottomLineColor = Colors.Black;
xfData.TopLineStyle = 1;
xfData.TopLineColor = Colors.Black;
xfData.LeftLineStyle = 1;
xfData.LeftLineColor = Colors.Black;
xfData.RightLineStyle = 1;
xfData.RightLineColor = Colors.Black;
#endregion
#region 填充数据
int i = 5;//从第五行开始为数据行
double provideTotal = 0;
double useTotal = 0;
double lossTotal = 0;
foreach(LineLossInfo lossItem in lossListExcel)
{
cells.Add(i,1,lossItem.Date,xfData);
if (Double.IsNaN(lossItem.ProvideValue))
cells.Add(i, 2, "-", xfData);
else
{
cells.Add(i, 2, lossItem.ProvideValue, xfData);
provideTotal += lossItem.ProvideValue;
}
if (Double.IsNaN(lossItem.UseValue))
cells.Add(i, 3, "-", xfData);
else
{
cells.Add(i, 3, lossItem.UseValue, xfData);
useTotal += lossItem.UseValue;
}
if (Double.IsNaN(lossItem.LossValue))
cells.Add(i, 4, "-", xfData);
else
{
cells.Add(i, 4, lossItem.LossValue, xfData);
lossTotal += lossItem.LossValue;
}
if(Double.IsNaN(lossItem.LossRate))
cells.Add(i,5,"-",xfData);
else
cells.Add(i,5,lossItem.LossRate,xfData);
if(String.IsNullOrEmpty(lossItem.Info))
cells.Add(i,6,"-",xfData);
else
cells.Add(i,6,lossItem.Info,xfData);
i++;
}
//添加总计
cells.Add(i, 2, "总计:", xfData);
cells.Add(i, 3, provideTotal, xfData);
cells.Add(i, 4, useTotal, xfData);
cells.Add(i, 5, lossTotal, xfData);
#endregion
string path=
//发送到客户端
xls.Send();
}