protected void Button1_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~/Report/");
string filename = path + "配电缺陷统计记录.xls";
object oReadOnly = true;
Excel._Workbook Myworkbook;
Excel._Application oExcel = new Excel.Application();
object missing = System.Reflection.Missing.Value;
Myworkbook = oExcel.Workbooks.Open(@filename, missing, oReadOnly, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
Excel.Worksheet Myworksheet = (Excel.Worksheet)Myworkbook.Worksheets["Sheet1"];
oExcel.Visible = true;
Myworksheet.Rows.AutoFit();
List<PmsModel.V_DePower> list = ViewState["list"] as List<PmsModel.V_DePower>; int Rindex1 = 5; int id = 1;
foreach (PmsModel.V_DePower model in list)
{
Myworksheet.Cells[Rindex1, 1] = id;
Myworksheet.Cells[Rindex1, 2] = (model.LineName + model.EquipmentName + model.DefectContent);
Myworksheet.Cells[Rindex1, 3] = model.DefectCategory;
Myworksheet.Cells[Rindex1, 4] = model.DiscoveryTime;
Myworksheet.Cells[Rindex1, 5] = model.DiscoveryMan;
Myworksheet.Cells[Rindex1, 6] = model.ApprovalSituation;
Myworksheet.Cells[Rindex1, 7] = model.OverhaulsTime;
Myworksheet.Cells[Rindex1, 8] = model.Overhauls;
Myworksheet.Cells[Rindex1, 9] = model.ApprovalTime;
Myworksheet.Cells[Rindex1, 10] = model.Approval;
Rindex1++;
id++;
}
string FullFileName = path + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls;
Myworksheet.SaveAs(FullFileName, missing, missing, missing, missing, missing, missing, missing, missing, missing);
Myworkbook.Saved = true;
oExcel.Workbooks.Close(); oExcel.Quit();
FileInfo DownloadFile = new FileInfo(FullFileName);
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
Response.Write(DownloadFile.FullName);
//Response.TransmitFile(DownloadFile.FullName); Response.Flush();
Response.End();
}点击导出的时候Excel只是闪一下,然后就存到我指定的文件夹Report中了,没有实现下载功能另存为, 这是为什么呀?
{
string path = Server.MapPath("~/Report/");
string filename = path + "配电缺陷统计记录.xls";
object oReadOnly = true;
Excel._Workbook Myworkbook;
Excel._Application oExcel = new Excel.Application();
object missing = System.Reflection.Missing.Value;
Myworkbook = oExcel.Workbooks.Open(@filename, missing, oReadOnly, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
Excel.Worksheet Myworksheet = (Excel.Worksheet)Myworkbook.Worksheets["Sheet1"];
oExcel.Visible = true;
Myworksheet.Rows.AutoFit();
List<PmsModel.V_DePower> list = ViewState["list"] as List<PmsModel.V_DePower>; int Rindex1 = 5; int id = 1;
foreach (PmsModel.V_DePower model in list)
{
Myworksheet.Cells[Rindex1, 1] = id;
Myworksheet.Cells[Rindex1, 2] = (model.LineName + model.EquipmentName + model.DefectContent);
Myworksheet.Cells[Rindex1, 3] = model.DefectCategory;
Myworksheet.Cells[Rindex1, 4] = model.DiscoveryTime;
Myworksheet.Cells[Rindex1, 5] = model.DiscoveryMan;
Myworksheet.Cells[Rindex1, 6] = model.ApprovalSituation;
Myworksheet.Cells[Rindex1, 7] = model.OverhaulsTime;
Myworksheet.Cells[Rindex1, 8] = model.Overhauls;
Myworksheet.Cells[Rindex1, 9] = model.ApprovalTime;
Myworksheet.Cells[Rindex1, 10] = model.Approval;
Rindex1++;
id++;
}
string FullFileName = path + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls;
Myworksheet.SaveAs(FullFileName, missing, missing, missing, missing, missing, missing, missing, missing, missing);
Myworkbook.Saved = true;
oExcel.Workbooks.Close(); oExcel.Quit();
FileInfo DownloadFile = new FileInfo(FullFileName);
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
Response.Write(DownloadFile.FullName);
//Response.TransmitFile(DownloadFile.FullName); Response.Flush();
Response.End();
}点击导出的时候Excel只是闪一下,然后就存到我指定的文件夹Report中了,没有实现下载功能另存为, 这是为什么呀?
解决方案 »
- 菜鸟求助,三层架构不太理解
- 【急】iframe 自适应高度问题
- 用C#调用oracle的存储过程时,出现“必须说明标识符”
- 还是刷新页面问题
- 问个小问题,请大家帮忙,如何得到总记录数.马上结贴
- 点击链接时如何像下载软件一样下载图片,紧急
- 请问如何记录下一个广告的点击数
- 求:各位介绍一本关于asp.net的高级编程的书籍(C#)。适用于中。高级用户。。。
- 关于repeater中显示文字变色问题,比较麻烦!高手来把
- 请问ASP.NET中如何使用TextBox,值总是无法更改
- 重装VS遇到个麻烦事,哪位大哥给指点一下
- asp.net 怎么调用“打开文件对话框”?是通过server.createobject("MSComDlg.CommonDialog")?
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
Response.TransmitFile(DownloadFile.FullName);
{
DateTime start = DateTime.Now;
Excel.Application app = new Excel.ApplicationClass();
app.Visible = false;
app.DisplayInfoWindow = false;
Excel.Workbooks books = app.Workbooks;
Excel.Workbook book = books.Open(Server.MapPath("Template/Allowance.xls"), Missing.Value, true, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Excel.Worksheet sheet = (Excel.Worksheet)book.Sheets[1]; DateTime end = DateTime.Now; try
{
int currentRow = 1;
string[] data = new string[table.Columns.Count];
foreach (DataRow row in table.Rows)
{
for (int i = 0; i < row.ItemArray.Length; i++)
{
data[i] = row.ItemArray[i].ToString();
}
currentRow++; sheet.Cells[currentRow, 1] = String.Join("|", data);
Excel.Range range = (Excel.Range)sheet.Cells[currentRow, 1];
range.TextToColumns(Missing.Value, Excel.XlTextParsingType.xlDelimited, Excel.XlTextQualifier.xlTextQualifierNone,
false, false, false, false, false, true, "|", Missing.Value, Missing.Value, Missing.Value, Missing.Value);
((Excel.Range)sheet.Cells[currentRow, 1]).AddComment(row.RowError); } sheet.UsedRange.Columns.AutoFit();
Excel.Borders borders = ((Excel.Borders)sheet.UsedRange.Borders);
borders.LineStyle = Excel.XlLineStyle.xlContinuous;
borders.Weight = Excel.XlBorderWeight.xlThin;
////凍結儲存格
//((Excel.Range)sheet.Cells[3, 4]).Select();
((Excel.Range)sheet.Cells[2, 4]).Select();
app.ActiveWindow.FreezePanes = true; string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "_Allowance.xls";
book.SaveAs(Server.MapPath("Download/" + fileName), Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlExclusive, Excel.XlSaveConflictResolution.xlLocalSessionChanges,
Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Response.Redirect("Download/" + fileName, false);
}
finally
{
book.Close(true, Missing.Value, Missing.Value);
books.Close();
app.Quit();
foreach (System.Diagnostics.Process excelProcess in System.Diagnostics.Process.GetProcessesByName("EXCEL"))
{
if (excelProcess.StartTime > start && excelProcess.StartTime < end)
{
excelProcess.Kill();
}
}
}
}
public void CreateExcel(DataSet ds, string FileName)
{
HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
string colHeaders = "", ls_item = "";
//定义表对象与行对象,同时用DataSet对其值进行初始化
DataTable dt = ds.Tables[0];
DataRow[] myRow = dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的
int i = 0;
int cl = dt.Columns.Count;
//取得数据表各列标题,各标题之间以t分割,最后一个列标题后加回车符
for (i = 0; i < cl; i++)
{
if (i == (cl - 1))//最后一列,加n
{
colHeaders += dt.Columns[i].Caption.ToString() + "\n";
}
else
{
colHeaders += dt.Columns[i].Caption.ToString() + "\t";
}
}
resp.Write(colHeaders);
//向HTTP输出流中写入取得的数据信息
//逐行处理数据
foreach (DataRow row in myRow)
{
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
for (i = 0; i < cl; i++)
{
if (i == (cl - 1))//最后一列,加n
{
ls_item += row[i].ToString() + "\n";
}
else
{
ls_item += row[i].ToString() + "\t";
}
}
resp.Write(ls_item);
ls_item = "";
}
resp.End();
}