各位, 我有一个datagrid, 有几列boundcolumn和一个hyperlinkcolumn, 在网上搜了好久也全都试过了, 保存为excel那个窗口就是不出现, 点完导出到excel的按钮之后鼠标一直是等待的漏斗状态(我试过等超过30分钟还没出现>_<). 各位帮忙看看代码有没有什么问题. 谢谢了private void btnExcel_Click(object sender, System.EventArgs e)
{
Response.Clear();
Response.Buffer= true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment; filename=invoice_list.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
dgrdList.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
{
Response.Clear();
Response.Buffer= true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment; filename=invoice_list.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
dgrdList.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
解决方案 »
- 关于接口的一些疑问,求解答!!
- 请问做C#开发,有必要学C++或者Java吗?
- 关于Dictionary的用法
- 实现datagirdview分页,应该用什么方法合适?
- 怎么用CE程序备份和还原数据库?
- 虚拟主机 服务器选择Win2000好呢,还是Win2003好(放置asp.net程序)
- 各位大侠,救救吧,折腾一中午啦。System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中
- C#中怎样让字符串中的多个空格转换成一个空格
- 绑定combobox
- WinForm Activate(); 调用无效,只是在任务栏闪烁。求大神
- 怎样较快的合理学习用.net开发个网站
- 两矩形算法
Dim con As New SqlClient.SqlConnection
con = data.DBConnection
Dim ds As DataSet = New DataSet
Dim sda As SqlDataAdapter = New SqlDataAdapter(Label1.Text, con)//label1放着sql语句
sda.Fill(ds, "t1") Dim dt As DataTable = ds.Tables("t1")
Dim sw As New System.IO.StringWriter sw.WriteLine("学号" + Chr(9).ToString() + "姓名" + Chr(9).ToString() + ........)//excel表头
Dim dr As DataRow For Each dr In dt.Rows sw.WriteLine(dr("学号") + Chr(9).ToString() + dr("姓名") + Chr(9).ToString() + ......)//读到sql中的数据
Next
sw.Close()
Response.AddHeader("Content-Disposition", "attachment; filename=wz_output_" + Date.Today.ToString("yyyyMMdd") + ".xls")
Response.ContentType = "application/ms-excel"
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312")
Response.Write(sw)
Response.End() DataGrid1.DataBind()
con.Close()
End Sub
试试这种方法看看行吧!!
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
试一下这个方法
Export("application/ms-excel", "Employee information.xls");调用 把GridView1换成你的dgrdList
public static void ExportTOExcel(DataGridView gridView, ToolStripProgressBar toolStripProgressBar1, string sN)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
if (gridView.Rows.Count == 0)
{
MessageBox.Show("没有数据可供导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出文件保存路径";
saveFileDialog.ShowDialog();
string strName = saveFileDialog.FileName;
if (strName.Length != 0)
{
System.Reflection.Missing miss = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
excel.Application.Workbooks.Add(true); ;
excel.Visible = false;//若是true,则在导出的时候会显示EXcel界面。
if (excel == null)
{
MessageBox.Show("EXCEL无法启动!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Microsoft.Office.Interop.Excel.Workbooks books = (Microsoft.Office.Interop.Excel.Workbooks)excel.Workbooks;
Microsoft.Office.Interop.Excel.Workbook book = (Microsoft.Office.Interop.Excel.Workbook)(books.Add(miss));
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet;
sheet.Name = sN; //生成字段名称
for (int i = 0; i < gridView.ColumnCount; i++)
{
excel.Cells[1, i + 1] = gridView.Columns[i].HeaderText.ToString();
}
//填充数据
toolStripProgressBar1.Maximum = gridView.Rows.Count - 1;
toolStripProgressBar1.Value = 0;
for (int i = 0; i < gridView.RowCount - 1; i++)
{
for (int j = 0; j < gridView.ColumnCount; j++)
{
if (gridView[j, i].Value == typeof(string))
{
if (gridView[i, j].Value != null)
{
excel.Cells[i + 2, j + 1] = "" + gridView[i, j].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = "";
}
}
else
{
if (gridView[j, i].Value != null)
{
excel.Cells[i + 2, j + 1] = gridView[j, i].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = "";
}
}
}
toolStripProgressBar1.Value = i;
}
sheet.SaveAs(strName, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss);
book.Close(false, miss, miss);
books.Close();
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
System.Runtime.InteropServices.Marshal.ReleaseComObject(books);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
GC.Collect();
MessageBox.Show("数据已经成功导出到:" + saveFileDialog.FileName.ToString(), "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
toolStripProgressBar1.Value = 0;
}
}
}
调用cls_Public.ExportTOExcel(dgvList1, tspb, dtpDate.Text + "工资表");
{
throw new Exception("当前列表中没有数据!");
}
// 当前对话
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
// IO用于导出并返回excel文件
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null; if (dgData != null)
{
// 设置编码和附件格式
curContext.Response.ContentType = "application/vnd.ms-excel";
//curContext.Response.ContentEncoding =System.Text.Encoding.UTF8;
curContext.Response.Charset ="utf-8";
curContext.Response.AppendHeader("Content-Disposition","attachment;filename=PortLog.xls");
// 导出excel文件
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
// 返回客户端
dgData.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString());
curContext.Response.End();
}
你到这个博客上看看有关于gridview导出Execl的具体代码!