mvc中,用chrome浏览器导出excel提示如题错误的解决办法。

<system.web>

<httpRuntime requestValidationMode="2.0"  targetFramework="4.5" />

<pages validateRequest="false" >

[ValidateInput(false)]
public ActionResult GridToExcel()
{
if (Request["ExportContent"] != "")
{
string listname = Request["listname"];
string sheetname = Request["sheetname"];
string tmpContent = Request["ExportContent"];//获取传递上来的文件内容
DataTable dt = Utility.ToDataTable(tmpContent);////rptSource.Tables[0];
//ExcelHelper.DataTable3Excel(dt, RptName);
Workbook b = FileExportExcel.OutFileToDisk(dt, listname, sheetname);
string filename = listname + "_" + DateTime.Now.ToString("yyyyMMddHHiiss");
return File(b.SaveToStream().ToArray(), "application/octet-stream", filename + ".xls");
}
else
{
return null;
}
//test();
//return null;
}

  

  public static DataTable ToDataTable(string json)
{
DataTable dataTable = new DataTable(); //实例化
DataTable result;
try
{
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);
if (arrayList.Count > )
{
foreach (Dictionary<string, object> dictionary in arrayList)
{
if (dictionary.Keys.Count<string>() == )
{
result = dataTable;
return result;
}
if (dataTable.Columns.Count == )
{
string s = "";
Type aa = s.GetType();
double f = 0.001d;
Type bb = f.GetType();
int i = ;
Type cc = i.GetType();
foreach (string current in dictionary.Keys)
{
if (object.Equals(dictionary[current], null))
dataTable.Columns.Add(current, aa);
else
dataTable.Columns.Add(current, dictionary[current].GetType() == cc ? bb : dictionary[current].GetType());
}
}
DataRow dataRow = dataTable.NewRow();
foreach (string current in dictionary.Keys)
{
dataRow[current] = dictionary[current];
} dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
}
}
}
catch (Exception e)
{
}
result = dataTable;
return result;
}
using Aspose.Cells; 

public static Workbook OutFileToDisk(DataTable dt, string tableName, string sheetname = "", string path = "")
{ Workbook workbook = new Workbook(); //工作簿
Worksheet sheet = workbook.Worksheets[]; //工作表\
if (!string.IsNullOrEmpty(sheetname))
{
sheet.Name = sheetname;
}
Cells cells = sheet.Cells;//单元格 //为标题设置样式
Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增样式
styleTitle.HorizontalAlignment = TextAlignmentType.Center;//文字居中
styleTitle.Font.Name = "宋体";//文字字体
styleTitle.Font.Size = ;//文字大小
styleTitle.Font.IsBold = true;//粗体 //样式2
Style style2 = workbook.Styles[workbook.Styles.Add()];//新增样式
style2.HorizontalAlignment = TextAlignmentType.Center;//文字居中
//style2.Font.Name = "宋体";//文字字体
//style2.Font.Size = 14;//文字大小
style2.Font.IsBold = true;//粗体
style2.IsTextWrapped = true;//单元格内容自动换行
style2.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
style2.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
style2.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
style2.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; //样式3
Style style3 = workbook.Styles[workbook.Styles.Add()];//新增样式
//style3.HorizontalAlignment = TextAlignmentType.Center;//文字居中
style3.Font.Name = "宋体";//文字字体
//style3.Font.Size = 12;//文字大小
style3.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
style3.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
style3.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
style3.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; int Colnum = dt.Columns.Count;//表格列数
int Rownum = dt.Rows.Count;//表格行数
//插入图片 //FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("~/Scripts/Image") + "/excelImage.png", FileMode.Open);
//byte[] byteData = new byte[fs.Length];
//fs.Read(byteData, 0, byteData.Length);
//fs.Close();
//cells.Merge(0, 0, 1, Colnum);//合并单元格
//cells[0, 0].PutValue(byteData);//填写内容
//cells.SetRowHeight(0, 38);
sheet.Pictures.Add(, , HttpContext.Current.Server.MapPath("~/Scripts/Image") + "/excelImage.png", , );
//生成行1 标题行
cells.Merge(, , , Colnum);//合并单元格
cells[, ].PutValue(tableName);//填写内容
cells[, ].SetStyle(styleTitle);
cells.SetRowHeight(, ); //生成行2 列名行
for (int i = ; i < Colnum; i++)
{
cells[, i].PutValue(dt.Columns[i].ColumnName);
cells[, i].SetStyle(style2);
//cells.SetRowHeight(1, 25);
} //生成数据行
for (int i = ; i < Rownum; i++)
{
for (int k = ; k < Colnum; k++)
{
cells[ + i, k].PutValue(dt.Rows[i][k]);//.ToString()
cells[ + i, k].SetStyle(style3);
}
//cells.SetRowHeight(2 + i, 24);
} return workbook;
//workbook.Save(path);
}
    function GridtoExcel(grid, listname, sheetname) {
var records = [];
var colu = [];
for (var i = 1; i < grid.columns.length; i++) {
if ((grid.columns[i].text === "") || (grid.columns[i].getId() === "")) {
}
else {
var dic = new Object();
dic.text = grid.columns[i].text;
dic.value = grid.columns[i].dataIndex;
colu.push(dic);
}
}
if (grid.getStore().data.items.length > 0) { for (var i = 0; i < grid.getStore().data.items.length; i++) {
var object = new Object;
var item = grid.getStore().data.items[i].data;
for (var j = 0; j < colu.length; j++) {
var daIndex = eval([colu[j].value]);
object[colu[j].text] = item[daIndex[0]];
}
records.push(object);
}
}
else {
var object = new Object;
for (var j = 0; j < colu.length; j++) {
object[colu[j].text] = null;
}
records.push(object);
} if (records.length > 0) {
var tempForm = document.createElement("form");
tempForm.action = '/Home/GridToExcel';
tempForm.method = "POST";
tempForm.target = "_blank";
var hideInput = document.createElement("input");
hideInput.type = "hidden";
hideInput.name = "ExportContent"
hideInput.value = Ext.JSON.encode(records);
tempForm.appendChild(hideInput);
var hideInput1 = document.createElement("input");
hideInput1.type = "hidden";
hideInput1.name = "listname"
hideInput1.value = listname;
tempForm.appendChild(hideInput1); var hideInput2 = document.createElement("input");
hideInput2.type = "hidden";
hideInput2.name = "sheetname"
hideInput2.value = sheetname;
tempForm.appendChild(hideInput2); tempForm.style.display = 'none';
// tempForm.get(0).encoding = 'application/json';
document.body.appendChild(tempForm);
tempForm.submit();
document.body.removeChild(tempForm);
}
else {
//
} }

最新文章

  1. 网络抓包工具-Wireshark学习资料
  2. 基于VC的声音文件操作(四)
  3. 《Pro Git》笔记2:Git基础操作
  4. AndEngine
  5. Java Api与HBase交互实例
  6. poj 1696 Space Ant(模拟+叉积)
  7. 在LINUX中跟踪函数调用----http://stackoverflow.com/
  8. 【百度地图API】如何调整结果面板的样式?如何获取指定页码的结果?
  9. AngularJS学习笔记(一)——一些基本知识
  10. Intel CPU命名规则的简略解析
  11. Swift中集合类型indexOf(Element)提示错误的解决办法
  12. HDU 6214.Smallest Minimum Cut 最少边数最小割
  13. linux就该这么学,第六天了
  14. Mac下Tomcat安装与Intellij IDEA配置Tomcat
  15. 吴裕雄 09-MySQL删除数据表
  16. oracle死锁解决方法
  17. Android Studio 出现 Build gradle project info
  18. Linux性能监控命令——sar
  19. AUC理解
  20. 企业招聘:UX设计师需要满足他们哪些期望?

热门文章

  1. Linux QA
  2. 嵌入式tcpip
  3. PAT1033
  4. PAT1028
  5. 【Luogu】P2488工作安排(费用流)
  6. PHP网站提交表单如何实现验证码验证功能
  7. script error总结
  8. Bridges
  9. Linux(Centos) 搭建ReviewBoard
  10. 关于fixed定位的一些错误看法纠正