谁知道为什么我导出的excel表格后,打开却是共享模式打开的,好多功能不能用?
如图:正常 的就是这样的:什么原因呢?
我的代码如下:
protected void Button3_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
FirstBindData(); ; try
{
if (conn != null)
{
cmd.Dispose();
conn.Close();
} //文件路径
String strFilePath = "\\excel\\";
String strFileName = DateTime.Now.ToString("yyyyMMdd") + ".xlsx";
//判断是不是null
if (xlApp == null)
{
Response.Write("<script>alert('创建Excel失败!')</script>");
return;
} Object ms = Type.Missing;
Microsoft.Office.Interop.Excel.Workbook myBook = xlApp.Workbooks.Add(ms);
Microsoft.Office.Interop.Excel.Worksheet mySheet = myBook.Worksheets[1] as Microsoft.Office.Interop.Excel.Worksheet;
//自动调整列宽
mySheet.Columns.EntireColumn.ColumnWidth = 15; for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
{
for (int j = 0; j < ds.Tables[1].Columns.Count; j++)
{
mySheet.Cells[i + 2, j + 1] = ds.Tables[1].Rows[i][j].ToString(); }
} if (System.IO.File.Exists(this.MapPath(".") + strFilePath + strFileName))
{
System.IO.File.Delete(this.MapPath(".") + strFilePath + strFileName);
}
String strXlsPath = this.MapPath(".") + strFilePath + strFileName;
myBook.SaveAs(strXlsPath, ms, ms, ms, ms, ms, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, ms, ms, ms, ms, ms);
xlApp.Quit();
System.IO.FileInfo file = new System.IO.FileInfo(strXlsPath);
Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", file.Length.ToString()); // 指定返回的是一个不能被客户端读取的流,必须被下载
Response.ContentType = "application/ms-excel"; // 把文件流发送到客户端
Response.WriteFile(file.FullName);
;
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch (Exception error)
{
Response.Write(error.ToString());
//释放Excel资源
xlApp.Quit();
if (conn != null)
{
cmd.Dispose();
conn.Close();
}
Response.Write("<script>alert('创建Excel失败!')</script>");
}
}excel导出exception
如图:正常 的就是这样的:什么原因呢?
我的代码如下:
protected void Button3_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
FirstBindData(); ; try
{
if (conn != null)
{
cmd.Dispose();
conn.Close();
} //文件路径
String strFilePath = "\\excel\\";
String strFileName = DateTime.Now.ToString("yyyyMMdd") + ".xlsx";
//判断是不是null
if (xlApp == null)
{
Response.Write("<script>alert('创建Excel失败!')</script>");
return;
} Object ms = Type.Missing;
Microsoft.Office.Interop.Excel.Workbook myBook = xlApp.Workbooks.Add(ms);
Microsoft.Office.Interop.Excel.Worksheet mySheet = myBook.Worksheets[1] as Microsoft.Office.Interop.Excel.Worksheet;
//自动调整列宽
mySheet.Columns.EntireColumn.ColumnWidth = 15; for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
{
for (int j = 0; j < ds.Tables[1].Columns.Count; j++)
{
mySheet.Cells[i + 2, j + 1] = ds.Tables[1].Rows[i][j].ToString(); }
} if (System.IO.File.Exists(this.MapPath(".") + strFilePath + strFileName))
{
System.IO.File.Delete(this.MapPath(".") + strFilePath + strFileName);
}
String strXlsPath = this.MapPath(".") + strFilePath + strFileName;
myBook.SaveAs(strXlsPath, ms, ms, ms, ms, ms, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, ms, ms, ms, ms, ms);
xlApp.Quit();
System.IO.FileInfo file = new System.IO.FileInfo(strXlsPath);
Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", file.Length.ToString()); // 指定返回的是一个不能被客户端读取的流,必须被下载
Response.ContentType = "application/ms-excel"; // 把文件流发送到客户端
Response.WriteFile(file.FullName);
;
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch (Exception error)
{
Response.Write(error.ToString());
//释放Excel资源
xlApp.Quit();
if (conn != null)
{
cmd.Dispose();
conn.Close();
}
Response.Write("<script>alert('创建Excel失败!')</script>");
}
}excel导出exception
解决方案 »
- 邮件问题
- 无法加载 DLL“LicenseCheck.dll”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。
- C# Winform中文本框的问题,为何在设置为Multiline模式后,粘贴如 "张三"<[email protected]> 这样的文本,总是未达到行尾提前换行?
- 怎样判断我提交的数据失败 和成功!!
- 怎么限定文本框只能输大写字母?
- 冰天雪地跪求答案:那位达人能将这段代码转换成C#~~ 急急急~~~
- 关于Activator类
- ASP.NET的大问题,不能不看的。
- 一个需要大量内存操作的计算,有没有什么方法能像C++里那样对虚拟内存进行管理的?
- 如何在 C# 中获得 Excel 工作表的一列
- 多客户端与一个服务器端的多线程问题
- 如何创建“Txt”文件,并在“.Txt”文件中写入信息
在Xp下载的Excel没有这情况
应该是系统原因
什么UNC,权限等等
objWorkbook.SaveAs(fileNameString, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Excel.XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
保存时 用这个 参数 【Excel.XlSaveAsAccessMode.xlExclusive 】
‘不’要用 【Excel.XlSaveAsAccessMode.xlShared】