我在做导出EXCEL时,代码在服务器端生成了一个对应EXCEL文件,想要下载完了删除这个文件,但是删除文件的代码不知道放什么地方,在这里的代码最后一行是Response.End(),在这行后不能放东西了 //
// TODO: 在此处添加构造函数逻辑
//
//dv为要输出到Excel的数据,str为标题名称
Double SumValue;
SumValue = 0; GC.Collect(); Excel.Application excel;// = new Application();
int rowIndex = 4;
int colIndex = 0; Excel.Workbook xBk;
Excel.Worksheet xSt; excel = new Excel.ApplicationClass(); xBk = excel.Workbooks.Add(true); xSt = (Excel.Worksheet)xBk.ActiveSheet;
excel.Cells.Font.Size = 10;
xSt.PageSetup.PaperSize = Excel.XlPaperSize.xlPaperA4; //A4纸
xSt.PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape; //横向打印
xSt.PageSetup.LeftMargin = 0.1; //左边界1
xSt.PageSetup.RightMargin = 0.1; //右边界1
xSt.PageSetup.CenterHorizontally = true;
int ManagerMoneyColIndex; //项目管理费列索引
ManagerMoneyColIndex = -1;
//
//取得标题
//
foreach (DataColumn col in dv.Table.Columns)
{
colIndex++;
excel.Cells[4, colIndex] = col.ColumnName;
xSt.get_Range(excel.Cells[4, colIndex], excel.Cells[4, colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐
if (col.ColumnName == "项目管理费")
ManagerMoneyColIndex = colIndex;
} //
//取得表格中的数据
//
foreach (DataRowView row in dv)
{
rowIndex++;
colIndex = 0;
SumValue = 0;
foreach (DataColumn col in dv.Table.Columns)
{
colIndex++; if (colIndex > 2 && colIndex < dv.Table.Columns.Count - 1)
{
SumValue = SumValue + Convert.ToDouble(row[colIndex].ToString());
} if (col.DataType == System.Type.GetType("System.DateTime"))
{
excel.Cells[rowIndex, colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
}
else
{
if ((colIndex >= 4) && (rowIndex > 4) && (colIndex <= dv.Table.Columns.Count-1))
excel.Cells[rowIndex, colIndex] = Convert.ToDouble(row[col.ColumnName].ToString()).ToString("N2");
else
{
if (col.DataType == System.Type.GetType("System.String"))
{
excel.Cells[rowIndex, colIndex] = "'" + row[col.ColumnName].ToString();
xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
}
else
{
excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
}
}
}
} if (ViewState["IsCanSeeManageMoney"].ToString() == "0")
{
if ((row[0].ToString() == "核发资金") && (row[1].ToString() != " "))
{
excel.Cells[rowIndex, ManagerMoneyColIndex] = " ";
}
}
excel.Cells[rowIndex, dv.Table.Columns.Count] = SumValue.ToString("N2");
}
//
//设置整个报表的标题为跨列居中
//
xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, colIndex]).Select();
xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, colIndex]).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenterAcrossSelection;
xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, colIndex]).Select();
xSt.get_Range(excel.Cells[4, 1], excel.Cells[rowSum, colIndex]).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenterAcrossSelection;
int sameindex;
string Merg;
Merg = xSt.get_Range(excel.Cells[5, 1], excel.Cells[5, 1]).Value2.ToString();
excel.DisplayAlerts = false;
for (sameindex = 5; sameindex < rowSum; sameindex++)
{
if (Merg == xSt.get_Range(excel.Cells[sameindex + 1, 1], excel.Cells[sameindex + 1, 1]).Value2.ToString())
{ xSt.get_Range(excel.Cells[sameindex, 1], excel.Cells[sameindex + 1, 1]).MergeCells = true; }
else
Merg = xSt.get_Range(excel.Cells[sameindex + 1, 1], excel.Cells[sameindex + 1, 1]).Value2.ToString();
}
excel.DisplayAlerts = true;
string FileName;
//FileName = DisPend(ProName);
FileName = DisPend(ProId);
xBk.SaveCopyAs(Server.MapPath(".") + "\\" + FileName + "核发明细表" + ".xls"); xBk.Close(false, null, null); excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
xBk = null;
excel = null;
xSt = null;
GC.Collect();
string path = Server.MapPath(FileName + "核发明细表" + ".xls");
System.IO.FileInfo file = new System.IO.FileInfo(path);
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);
Response.End();
// TODO: 在此处添加构造函数逻辑
//
//dv为要输出到Excel的数据,str为标题名称
Double SumValue;
SumValue = 0; GC.Collect(); Excel.Application excel;// = new Application();
int rowIndex = 4;
int colIndex = 0; Excel.Workbook xBk;
Excel.Worksheet xSt; excel = new Excel.ApplicationClass(); xBk = excel.Workbooks.Add(true); xSt = (Excel.Worksheet)xBk.ActiveSheet;
excel.Cells.Font.Size = 10;
xSt.PageSetup.PaperSize = Excel.XlPaperSize.xlPaperA4; //A4纸
xSt.PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape; //横向打印
xSt.PageSetup.LeftMargin = 0.1; //左边界1
xSt.PageSetup.RightMargin = 0.1; //右边界1
xSt.PageSetup.CenterHorizontally = true;
int ManagerMoneyColIndex; //项目管理费列索引
ManagerMoneyColIndex = -1;
//
//取得标题
//
foreach (DataColumn col in dv.Table.Columns)
{
colIndex++;
excel.Cells[4, colIndex] = col.ColumnName;
xSt.get_Range(excel.Cells[4, colIndex], excel.Cells[4, colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐
if (col.ColumnName == "项目管理费")
ManagerMoneyColIndex = colIndex;
} //
//取得表格中的数据
//
foreach (DataRowView row in dv)
{
rowIndex++;
colIndex = 0;
SumValue = 0;
foreach (DataColumn col in dv.Table.Columns)
{
colIndex++; if (colIndex > 2 && colIndex < dv.Table.Columns.Count - 1)
{
SumValue = SumValue + Convert.ToDouble(row[colIndex].ToString());
} if (col.DataType == System.Type.GetType("System.DateTime"))
{
excel.Cells[rowIndex, colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
}
else
{
if ((colIndex >= 4) && (rowIndex > 4) && (colIndex <= dv.Table.Columns.Count-1))
excel.Cells[rowIndex, colIndex] = Convert.ToDouble(row[col.ColumnName].ToString()).ToString("N2");
else
{
if (col.DataType == System.Type.GetType("System.String"))
{
excel.Cells[rowIndex, colIndex] = "'" + row[col.ColumnName].ToString();
xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
}
else
{
excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
}
}
}
} if (ViewState["IsCanSeeManageMoney"].ToString() == "0")
{
if ((row[0].ToString() == "核发资金") && (row[1].ToString() != " "))
{
excel.Cells[rowIndex, ManagerMoneyColIndex] = " ";
}
}
excel.Cells[rowIndex, dv.Table.Columns.Count] = SumValue.ToString("N2");
}
//
//设置整个报表的标题为跨列居中
//
xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, colIndex]).Select();
xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, colIndex]).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenterAcrossSelection;
xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, colIndex]).Select();
xSt.get_Range(excel.Cells[4, 1], excel.Cells[rowSum, colIndex]).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenterAcrossSelection;
int sameindex;
string Merg;
Merg = xSt.get_Range(excel.Cells[5, 1], excel.Cells[5, 1]).Value2.ToString();
excel.DisplayAlerts = false;
for (sameindex = 5; sameindex < rowSum; sameindex++)
{
if (Merg == xSt.get_Range(excel.Cells[sameindex + 1, 1], excel.Cells[sameindex + 1, 1]).Value2.ToString())
{ xSt.get_Range(excel.Cells[sameindex, 1], excel.Cells[sameindex + 1, 1]).MergeCells = true; }
else
Merg = xSt.get_Range(excel.Cells[sameindex + 1, 1], excel.Cells[sameindex + 1, 1]).Value2.ToString();
}
excel.DisplayAlerts = true;
string FileName;
//FileName = DisPend(ProName);
FileName = DisPend(ProId);
xBk.SaveCopyAs(Server.MapPath(".") + "\\" + FileName + "核发明细表" + ".xls"); xBk.Close(false, null, null); excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
xBk = null;
excel = null;
xSt = null;
GC.Collect();
string path = Server.MapPath(FileName + "核发明细表" + ".xls");
System.IO.FileInfo file = new System.IO.FileInfo(path);
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);
Response.End();
要有删除的权限。
strPa=Server.MapPath(".") + "\\" + FileName + "核发明细表" + ".xls";
if(File.Exists(strPa))
{
File.Delete(strPa);
}
或用多线程,先下载再延迟一段时间再删除文件
strPa=Server.MapPath(".") + "\\" + FileName + "核发明细表" + ".xls";
if(File.Exists(strPa))
{
File.Delete(strPa);
}
这段代码如果写到没有被执行到啊
GC.Collect();
string path = Server.MapPath(FileName + "核发明细表" + ".xls");//这两句是楼主的代码,下面是我的代码
FileStream fs = new FileStream(path, FileMode.Open);
byte[] buffer = new byte[fs.Length];
fs.Close();
File.Delete(path);
Response.ContentType = "application/ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + "核发明细表" + ".xls");
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.Flush();
Response.End();
fs.Read(buffer, 0, buffer.Length);
然后才是fs.Close();忙中出错
X0440906鏍稿彂鏄庣粏琛╗1].xls有什么办法可以显示原来的名字吗
Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + "核发明细表" + ".xls");
修改为:
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.Encode(FileName + "核发明细表" + ".xls"));
前面再加两句:
Response.Charset = "GB2312";
Response.ContentEncoding = Encoding.UTF8;
就可以了
File.Delete(string path)
http://www.cnblogs.com/insus/articles/1400266.html
这个临时文作夹内的文件,当天的文件删除不了,我们只能做一个动作每天删除昨天或以前的临时文件.
参考:
http://www.cnblogs.com/insus/articles/1431986.html