采用这种方式导出excel文件时,怎么制定sheet的名字?

阳光正好2024 2010-02-09 03:57:30
Response.Clear();
Response.Buffer = true;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode("导出客户列表", System.Text.Encoding.UTF8) + ".xls");
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
System.Web.UI.WebControls.GridView dataGrid = new System.Web.UI.WebControls.GridView();
dataGrid.DataSource = dt;
dataGrid.RowDataBound += new GridViewRowEventHandler(dataGrid_RowDataBound);
dataGrid.DataBind();
dataGrid.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
...全文
763 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
richie8398 2010-02-10
  • 打赏
  • 举报
回复
引用 3 楼 qq497525725 的回复:
            object oMissiong = System.Reflection.Missing.Value;
            string ErrMsg = string.Empty;
            string Title = "Excel中的标题";
            //创建Excel对象
            Microsoft.Office.Interop.Excel.ApplicationClass acExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
            Microsoft.Office.Interop.Excel.Workbook wb = acExcel.Workbooks.Add(true);
            //不显示Excel
            acExcel.DisplayAlerts = false;
            Microsoft.Office.Interop.Excel.Sheets mysheet = null;
            mysheet = wb.Sheets;
            try
            {
                if (acExcel == null)
                {
                    ErrMsg = "Error:请确定是否安装了Excel";
                }
                acExcel.Visible = false;

                //先加入sheet
                for (int i = 0; i < arr.Count; i++)
                {
                    mysheet.Add(oMissiong, oMissiong, 1, oMissiong);
                }
                //删除多余的sheet,创建Excel时候会自动生成一个空白的sheet
                for (int i = 0; i < wb.Sheets.Count; i++)
                {
                    if (i == wb.Sheets.Count - 1)
                    {
                        Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)mysheet.get_Item(i + 1);
                        ws.Delete();
                    }
                }

                for (int i = 0; i < arr.Count; i++)
                {
                    //Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.ActiveSheet;
                    Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)mysheet.get_Item(i + 1);
                    ws.Name = "sheet的名称";
                    ws.PageSetup.CenterHorizontally = true;                  // 设置水平居中
                    Microsoft.Office.Interop.Excel.Range range = null;

                    }
                    //释放资源
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
                    ws = null;
                }
                // 保存文件到程序运行目录下
                wb.SaveCopyAs(Server.MapPath("../ExcelFile/" + FileName + ".xls"));
                wb.Close(false, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
                wb = null;
                acExcel.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(acExcel);
                acExcel = null;
                GC.Collect();
                GC.WaitForPendingFinalizers();

                // 设置编码 
                string path = Server.MapPath("../ExcelFile/" + FileName + ".xls");
                System.IO.FileInfo file = new System.IO.FileInfo(path);
                Res.Clear();
                Res.Charset = "GB2312";
                Res.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                // 指定返回的是一个不能被客户端读取的流,必须被下载
                Res.ContentType = "application/ms-excel";
                // 添加头信息,为"文件下载/另存为"对话框指定默认文件名
                Res.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
                //添加头信息,指定文件大小,让浏览器能够显示下载进度
                Res.AddHeader("Content-Length", file.Length.ToString());
                // 把文件流发送到客户端
                Res.WriteFile(path);
                // 停止页面的执行
                Res.Flush();
                Res.End();
                Message = ErrMsg;
            }
            catch (Exception ex)
            {
                Message = ErrMsg;
            }
            finally
            {
                foreach (System.Diagnostics.Process pro in System.Diagnostics.Process.GetProcessesByName("Excel"))
                    pro.Kill();
            }
            System.GC.SuppressFinalize(this);


你要脸吗,我的代码贴到自己这里,鄙视你!!!!
richie8398 2010-02-10
  • 打赏
  • 举报
回复
引用 2 楼 richie8398 的回复:
给你一个例子,应该能对你有所帮助!
C# codeobject oMissiong= System.Reflection.Missing.Value;string ErrMsg=string.Empty;string Title="Excel中的标题";//创建Excel对象 Microsoft.Office.Interop.Excel.ApplicationClass acExcel=new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook wb= acExcel.Workbooks.Add(true);//不显示Excel acExcel.DisplayAlerts=false;
Microsoft.Office.Interop.Excel.Sheets mysheet=null;
mysheet= wb.Sheets;try
{if (acExcel==null)
{
ErrMsg="Error:请确定是否安装了Excel";
}
acExcel.Visible=false;//先加入sheetfor (int i=0; i< arr.Count; i++)
{
mysheet.Add(oMissiong, oMissiong,1, oMissiong);
}//删除多余的sheet,创建Excel时候会自动生成一个空白的sheetfor (int i=0; i< wb.Sheets.Count; i++)
{if (i== wb.Sheets.Count-1)
{
Microsoft.Office.Interop.Excel.Worksheet ws= (Microsoft.Office.Interop.Excel.Worksheet)mysheet.get_Item(i+1);
ws.Delete();
}
}for (int i=0; i< arr.Count; i++)
{//Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.ActiveSheet; Microsoft.Office.Interop.Excel.Worksheet ws= (Microsoft.Office.Interop.Excel.Worksheet)mysheet.get_Item(i+1);
ws.Name="sheet的名称";
ws.PageSetup.CenterHorizontally=true;// 设置水平居中 Microsoft.Office.Interop.Excel.Range range=null;

}//释放资源 System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
ws=null;
}// 保存文件到程序运行目录下 wb.SaveCopyAs(Server.MapPath("../ExcelFile/"+ FileName+".xls"));
wb.Close(false, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
wb=null;
acExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(acExcel);
acExcel=null;
GC.Collect();
GC.WaitForPendingFinalizers();// 设置编码string path= Server.MapPath("../ExcelFile/"+ FileName+".xls");
System.IO.FileInfo file=new System.IO.FileInfo(path);
Res.Clear();
Res.Charset="GB2312";
Res.ContentEncoding= System.Text.Encoding.GetEncoding("GB2312");// 指定返回的是一个不能被客户端读取的流,必须被下载 Res.ContentType="application/ms-excel";// 添加头信息,为"文件下载/另存为"对话框指定默认文件名 Res.AddHeader("Content-Disposition","attachment; filename="+ System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));//添加头信息,指定文件大小,让浏览器能够显示下载进度 Res.AddHeader("Content-Length", file.Length.ToString());// 把文件流发送到客户端 Res.WriteFile(path);// 停止页面的执行 Res.Flush();
Res.End();
Message= ErrMsg;
}catch (Exception ex)
{
Message= ErrMsg;
}finally
{foreach (System.Diagnostics.Process proin System.Diagnostics.Process.GetProcessesByName("Excel"))
pro.Kill();
}
System.GC.SuppressFinalize(this);



你要脸吗,用别人的代码还贴到自己这里
缭绕飘渺 2010-02-09
  • 打赏
  • 举报
回复
正好学习了
我也用到了
qq497525725 2010-02-09
  • 打赏
  • 举报
回复
object oMissiong = System.Reflection.Missing.Value;
string ErrMsg = string.Empty;
string Title = "Excel中的标题";
//创建Excel对象
Microsoft.Office.Interop.Excel.ApplicationClass acExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook wb = acExcel.Workbooks.Add(true);
//不显示Excel
acExcel.DisplayAlerts = false;
Microsoft.Office.Interop.Excel.Sheets mysheet = null;
mysheet = wb.Sheets;
try
{
if (acExcel == null)
{
ErrMsg = "Error:请确定是否安装了Excel";
}
acExcel.Visible = false;

//先加入sheet
for (int i = 0; i < arr.Count; i++)
{
mysheet.Add(oMissiong, oMissiong, 1, oMissiong);
}
//删除多余的sheet,创建Excel时候会自动生成一个空白的sheet
for (int i = 0; i < wb.Sheets.Count; i++)
{
if (i == wb.Sheets.Count - 1)
{
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)mysheet.get_Item(i + 1);
ws.Delete();
}
}

for (int i = 0; i < arr.Count; i++)
{
//Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.ActiveSheet;
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)mysheet.get_Item(i + 1);
ws.Name = "sheet的名称";
ws.PageSetup.CenterHorizontally = true; // 设置水平居中
Microsoft.Office.Interop.Excel.Range range = null;

}
//释放资源
System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
ws = null;
}
// 保存文件到程序运行目录下
wb.SaveCopyAs(Server.MapPath("../ExcelFile/" + FileName + ".xls"));
wb.Close(false, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
wb = null;
acExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(acExcel);
acExcel = null;
GC.Collect();
GC.WaitForPendingFinalizers();

// 设置编码
string path = Server.MapPath("../ExcelFile/" + FileName + ".xls");
System.IO.FileInfo file = new System.IO.FileInfo(path);
Res.Clear();
Res.Charset = "GB2312";
Res.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
// 指定返回的是一个不能被客户端读取的流,必须被下载
Res.ContentType = "application/ms-excel";
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Res.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
//添加头信息,指定文件大小,让浏览器能够显示下载进度
Res.AddHeader("Content-Length", file.Length.ToString());
// 把文件流发送到客户端
Res.WriteFile(path);
// 停止页面的执行
Res.Flush();
Res.End();
Message = ErrMsg;
}
catch (Exception ex)
{
Message = ErrMsg;
}
finally
{
foreach (System.Diagnostics.Process pro in System.Diagnostics.Process.GetProcessesByName("Excel"))
pro.Kill();
}
System.GC.SuppressFinalize(this);

richie8398 2010-02-09
  • 打赏
  • 举报
回复
给你一个例子,应该能对你有所帮助!


object oMissiong = System.Reflection.Missing.Value;
string ErrMsg = string.Empty;
string Title = "Excel中的标题";
//创建Excel对象
Microsoft.Office.Interop.Excel.ApplicationClass acExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook wb = acExcel.Workbooks.Add(true);
//不显示Excel
acExcel.DisplayAlerts = false;
Microsoft.Office.Interop.Excel.Sheets mysheet = null;
mysheet = wb.Sheets;
try
{
if (acExcel == null)
{
ErrMsg = "Error:请确定是否安装了Excel";
}
acExcel.Visible = false;

//先加入sheet
for (int i = 0; i < arr.Count; i++)
{
mysheet.Add(oMissiong, oMissiong, 1, oMissiong);
}
//删除多余的sheet,创建Excel时候会自动生成一个空白的sheet
for (int i = 0; i < wb.Sheets.Count; i++)
{
if (i == wb.Sheets.Count - 1)
{
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)mysheet.get_Item(i + 1);
ws.Delete();
}
}

for (int i = 0; i < arr.Count; i++)
{
//Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.ActiveSheet;
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)mysheet.get_Item(i + 1);
ws.Name = "sheet的名称";
ws.PageSetup.CenterHorizontally = true; // 设置水平居中
Microsoft.Office.Interop.Excel.Range range = null;

}
//释放资源
System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
ws = null;
}
// 保存文件到程序运行目录下
wb.SaveCopyAs(Server.MapPath("../ExcelFile/" + FileName + ".xls"));
wb.Close(false, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
wb = null;
acExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(acExcel);
acExcel = null;
GC.Collect();
GC.WaitForPendingFinalizers();

// 设置编码
string path = Server.MapPath("../ExcelFile/" + FileName + ".xls");
System.IO.FileInfo file = new System.IO.FileInfo(path);
Res.Clear();
Res.Charset = "GB2312";
Res.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
// 指定返回的是一个不能被客户端读取的流,必须被下载
Res.ContentType = "application/ms-excel";
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Res.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
//添加头信息,指定文件大小,让浏览器能够显示下载进度
Res.AddHeader("Content-Length", file.Length.ToString());
// 把文件流发送到客户端
Res.WriteFile(path);
// 停止页面的执行
Res.Flush();
Res.End();
Message = ErrMsg;
}
catch (Exception ex)
{
Message = ErrMsg;
}
finally
{
foreach (System.Diagnostics.Process pro in System.Diagnostics.Process.GetProcessesByName("Excel"))
pro.Kill();
}
System.GC.SuppressFinalize(this);


阳光正好2024 2010-02-09
  • 打赏
  • 举报
回复
dingyixiadingyixiadingyixiadingyixiadingyixiadingyixiadingyixia

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧