string newpath = Server.MapPath(".") + @"\Excel\" + Guid.NewGuid() + ".xls";
FileInfo mode = new FileInfo(Server.MapPath(".") + @"\Excel\详细资料.xls");
Excel.Application app = new Excel.Application();
app.Application.DisplayAlerts = false;
app.Visible = false;
if (mode.Exists)
{
Excel.Workbook tworkbook;
Object missing = System.Reflection.Missing.Value;
app.Workbooks.Add(missing);
tworkbook = app.Workbooks.Open(mode.FullName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
for (int i = 0; i < this.GridView1.Rows.Count; i++)
{
Excel.Worksheet tworksheet = (Excel.Worksheet)tworkbook.Sheets[1];
tworksheet.Cells[3, 2] = this.GridView1.Rows[i].Cells[0].Text;
tworksheet.Cells[3, 7] = Convert.ToDateTime(this.GridView1.Rows[i].Cells[1].Text).ToShortDateString();
if (this.GridView1.Rows[i].Cells[2].Text == " ")
{
this.GridView1.Rows[i].Cells[2].Text = "";
}
tworksheet.Cells[4, 2] = this.GridView1.Rows[i].Cells[2].Text;
tworksheet.Cells[4, 4] = this.GridView1.Rows[i].Cells[3].Text;
tworksheet.Cells[4, 6] = this.GridView1.Rows[i].Cells[4].Text;
tworksheet.Cells[5, 2] = this.GridView1.Rows[i].Cells[5].Text;
tworksheet.Cells[5, 4] = this.GridView1.Rows[i].Cells[6].Text;
tworksheet.Cells[5, 6] = this.GridView1.Rows[i].Cells[7].Text;
tworksheet.Cells[6, 2] = this.GridView1.Rows[i].Cells[8].Text;
tworksheet.Cells[6, 4] = this.GridView1.Rows[i].Cells[9].Text;
tworksheet.Cells[6, 6] = this.GridView1.Rows[i].Cells[10].Text;
tworksheet.Cells[6, 8] = this.GridView1.Rows[i].Cells[11].Text;
tworksheet.Cells[7, 2] = this.GridView1.Rows[i].Cells[12].Text;
tworksheet.Cells[7, 4] = this.GridView1.Rows[i].Cells[13].Text;
tworksheet.Cells[8, 2] = this.GridView1.Rows[i].Cells[14].Text;
tworksheet.Cells[8, 4] = this.GridView1.Rows[i].Cells[15].Text;
tworksheet.Cells[8, 6] = this.GridView1.Rows[i].Cells[16].Text;
tworksheet.Cells[8, 8] = this.GridView1.Rows[i].Cells[17].Text;
tworksheet.Cells[9, 2] = this.GridView1.Rows[i].Cells[18].Text;
tworksheet.Cells[9, 4] = this.GridView1.Rows[i].Cells[19].Text;
tworksheet.Cells[9, 6] = this.GridView1.Rows[i].Cells[20].Text;
tworksheet.Cells[9, 8] = this.GridView1.Rows[i].Cells[21].Text;
tworksheet.Cells[10, 2] = this.GridView1.Rows[i].Cells[22].Text;
tworksheet.Cells[10, 4] = this.GridView1.Rows[i].Cells[23].Text;
tworksheet.Cells[10, 6] = this.GridView1.Rows[i].Cells[24].Text;
tworksheet.Cells[10, 8] = this.GridView1.Rows[i].Cells[25].Text;
tworksheet.Cells[11, 2] = this.GridView1.Rows[i].Cells[26].Text;
tworksheet.Cells[17, 2] = this.GridView1.Rows[i].Cells[27].Text;
tworksheet.SaveAs(newpath, missing, missing, missing, missing, missing, missing, missing, missing, missing);
}
tworkbook.Close(false, mode.FullName, missing);
app.Workbooks.Close();
app.Quit();
if (app != null)
{
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("Excel"))
{
//先判断当前进程是否是excel
if (!p.CloseMainWindow())
{
p.Kill();
}
}
}
tworkbook = null;
app = null;
GC.Collect();
}
//打开保存对话框
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.Charset = "UTF-8";
Response.ContentType = "application/ms-excel";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(mode.Name));
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("Content-Length", mode.Length.ToString());
Response.Charset = "";
this.EnableViewState = false;
Response.WriteFile(newpath);
//删除创建的Excel文件
FileInfo fileinf = new FileInfo(newpath);
fileinf.Delete();
//关闭连接
Response.Flush();
Response.End();这段代码里那有问题?为什么发布后打开Excel会说“不能访问“详细资料.xls”,该文件可能是只读的,或者您要访问的位置是只读的,或者文件所在的服务器没有响应”。我的权限都设定好了。我肯定是代码里的问题,请各位高手帮的给解决以下。急~~~~~
FileInfo mode = new FileInfo(Server.MapPath(".") + @"\Excel\详细资料.xls");
Excel.Application app = new Excel.Application();
app.Application.DisplayAlerts = false;
app.Visible = false;
if (mode.Exists)
{
Excel.Workbook tworkbook;
Object missing = System.Reflection.Missing.Value;
app.Workbooks.Add(missing);
tworkbook = app.Workbooks.Open(mode.FullName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
for (int i = 0; i < this.GridView1.Rows.Count; i++)
{
Excel.Worksheet tworksheet = (Excel.Worksheet)tworkbook.Sheets[1];
tworksheet.Cells[3, 2] = this.GridView1.Rows[i].Cells[0].Text;
tworksheet.Cells[3, 7] = Convert.ToDateTime(this.GridView1.Rows[i].Cells[1].Text).ToShortDateString();
if (this.GridView1.Rows[i].Cells[2].Text == " ")
{
this.GridView1.Rows[i].Cells[2].Text = "";
}
tworksheet.Cells[4, 2] = this.GridView1.Rows[i].Cells[2].Text;
tworksheet.Cells[4, 4] = this.GridView1.Rows[i].Cells[3].Text;
tworksheet.Cells[4, 6] = this.GridView1.Rows[i].Cells[4].Text;
tworksheet.Cells[5, 2] = this.GridView1.Rows[i].Cells[5].Text;
tworksheet.Cells[5, 4] = this.GridView1.Rows[i].Cells[6].Text;
tworksheet.Cells[5, 6] = this.GridView1.Rows[i].Cells[7].Text;
tworksheet.Cells[6, 2] = this.GridView1.Rows[i].Cells[8].Text;
tworksheet.Cells[6, 4] = this.GridView1.Rows[i].Cells[9].Text;
tworksheet.Cells[6, 6] = this.GridView1.Rows[i].Cells[10].Text;
tworksheet.Cells[6, 8] = this.GridView1.Rows[i].Cells[11].Text;
tworksheet.Cells[7, 2] = this.GridView1.Rows[i].Cells[12].Text;
tworksheet.Cells[7, 4] = this.GridView1.Rows[i].Cells[13].Text;
tworksheet.Cells[8, 2] = this.GridView1.Rows[i].Cells[14].Text;
tworksheet.Cells[8, 4] = this.GridView1.Rows[i].Cells[15].Text;
tworksheet.Cells[8, 6] = this.GridView1.Rows[i].Cells[16].Text;
tworksheet.Cells[8, 8] = this.GridView1.Rows[i].Cells[17].Text;
tworksheet.Cells[9, 2] = this.GridView1.Rows[i].Cells[18].Text;
tworksheet.Cells[9, 4] = this.GridView1.Rows[i].Cells[19].Text;
tworksheet.Cells[9, 6] = this.GridView1.Rows[i].Cells[20].Text;
tworksheet.Cells[9, 8] = this.GridView1.Rows[i].Cells[21].Text;
tworksheet.Cells[10, 2] = this.GridView1.Rows[i].Cells[22].Text;
tworksheet.Cells[10, 4] = this.GridView1.Rows[i].Cells[23].Text;
tworksheet.Cells[10, 6] = this.GridView1.Rows[i].Cells[24].Text;
tworksheet.Cells[10, 8] = this.GridView1.Rows[i].Cells[25].Text;
tworksheet.Cells[11, 2] = this.GridView1.Rows[i].Cells[26].Text;
tworksheet.Cells[17, 2] = this.GridView1.Rows[i].Cells[27].Text;
tworksheet.SaveAs(newpath, missing, missing, missing, missing, missing, missing, missing, missing, missing);
}
tworkbook.Close(false, mode.FullName, missing);
app.Workbooks.Close();
app.Quit();
if (app != null)
{
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("Excel"))
{
//先判断当前进程是否是excel
if (!p.CloseMainWindow())
{
p.Kill();
}
}
}
tworkbook = null;
app = null;
GC.Collect();
}
//打开保存对话框
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.Charset = "UTF-8";
Response.ContentType = "application/ms-excel";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(mode.Name));
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("Content-Length", mode.Length.ToString());
Response.Charset = "";
this.EnableViewState = false;
Response.WriteFile(newpath);
//删除创建的Excel文件
FileInfo fileinf = new FileInfo(newpath);
fileinf.Delete();
//关闭连接
Response.Flush();
Response.End();这段代码里那有问题?为什么发布后打开Excel会说“不能访问“详细资料.xls”,该文件可能是只读的,或者您要访问的位置是只读的,或者文件所在的服务器没有响应”。我的权限都设定好了。我肯定是代码里的问题,请各位高手帮的给解决以下。急~~~~~
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货