导出excel表格

一.

1.获取数据源
2.DataTable dt = st.Tables[0];
HttpResponse resp; // HTTP响应信息
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); //设置输出流的HTTP字符集
resp.ContentType = "application/vnd.ms-excel"; //设置输出流的HTTP MIME类型
string name = "Message_" + DateTime.Now.ToString("yyyyMMddhhss") + ".xls";
resp.AppendHeader("Content-Disposition", "attachment;filename=" + name); //将HTTP头添加到输出流

// 定义表对象与行对象,同时用DataSet对其值进行初始化
//
dt.Columns.Remove("lyqq");
dt.Columns.Remove("lyhf");
dt.Columns["lyid"].ColumnName = "编号";
dt.Columns["lyname2"].ColumnName = "姓名";
dt.Columns["lymail"].ColumnName = "邮箱";
dt.Columns["lydh"].ColumnName = "电话";
dt.Columns["lydz"].ColumnName = "地址";
dt.Columns["lyname"].ColumnName = "回访时间";
dt.Columns["lyneirong"].ColumnName = "内容";
dt.Columns["lytime"].ColumnName = "时间";
System.IO.StringWriter oSW = new System.IO.StringWriter();
HtmlTextWriter oHW = new HtmlTextWriter(oSW);
DataGrid dg = new DataGrid();
dg.DataSource = dt;
dg.DataBind();
dg.RenderControl(oHW);
resp.Write(oSW.ToString());
resp.Flush();
resp.Close();

二.带图片导出

private void OutExcel2()
{
datalist(); //获取数据
DataTable dt = st.Tables[0];
if (dt != null)
{
#region 操作excel
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
xlWorkBook = new Excel.Application().Workbooks.Add(Type.Missing);
xlWorkBook.Application.Visible = false;
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets[1]; //创建一个工作簿
//设置工作簿里显示的单元格的标题
xlWorkSheet.Cells[1, 1] = "名称";
xlWorkSheet.Cells[1, 2] = "简介";
xlWorkSheet.Cells[1, 3] = "间隔";
xlWorkSheet.Cells[1, 4] = "图片";
//设置宽度
((Excel.Range)xlWorkSheet.Cells[1, 2]).ColumnWidth = 15;
((Excel.Range)xlWorkSheet.Cells[1, 4]).ColumnWidth = 20;//图片的宽度
//列宽自动
// xlWorkSheet.get_Range(xlWorkSheet.Cells[1, 1], xlWorkSheet.Cells[1, columns]).EntireColumn.AutoFit();
// xlWorkSheet.Columns.EntireColumn.AutoFit();//自动适应长度
//设置字体
xlWorkSheet.Cells.Font.Size = 12;
xlWorkSheet.Cells.Rows.RowHeight = 100;
#region 为excel赋值
for (int i = 0; i < dt.Rows.Count; i++)
{
//为单元格赋值。
xlWorkSheet.Cells[i + 2, 1] = dt.Rows[i]["spname"].ToString();
xlWorkSheet.Cells[i + 2, 2] = dt.Rows[i]["spjj"].ToString();
xlWorkSheet.Cells[i + 2, 3] = dt.Rows[i]["spjg"].ToString();

#region
//直接取图片的地址
string filename = Server.MapPath(dt.Rows[i]["sptp"].ToString());
//用下面的方法把图片从数据库里取出来。
//byte[] filedata = (byte[])dtimg.Rows[j]["img"];
//System.IO.MemoryStream ms = new System.IO.MemoryStream(filedata);
//System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
//img.Save(filename);
#endregion
//int rangeindex = i+1;
//string rangename = "D" + rangeindex;
//Excel.Range range = xlWorkSheet.get_Range(rangename, Type.Missing);
//range.Select();
//Excel.Pictures pict = (Excel.Pictures)xlWorkSheet.Pictures(Type.Missing);
//pict.Insert(filename, Type.Missing);

//Left , Top , Width and Height.设置指定位置图片的显示
xlWorkSheet.Shapes.AddPicture(filename, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue,
220, 100 + i * 100, 100, 100);
}
#endregion
#region 保存excel文件
string filePath = Server.MapPath("ReadExcel") + "" + System.DateTime.Now.ToString().Replace(":", "") + ".xls";
xlWorkBook.SaveAs(filePath); //保存
xlWorkBook.Application.Quit(); //关闭 Excel.Workbook
xlWorkSheet = null;
xlWorkBook = null;
GC.Collect();//回收
System.GC.WaitForPendingFinalizers();
#endregion
#endregion
#region 导出到客户端
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode("导出", System.Text.Encoding.UTF8) + ".xls");
Response.ContentType = "Application/excel";
Response.WriteFile(filePath);
Response.End();
#endregion
KillProcessexcel("EXCEL");
}
}

#region 杀死进程
private void KillProcessexcel(string processName)
{ //获得进程对象,以用来操作
System.Diagnostics.Process myproc = new System.Diagnostics.Process();
//得到所有打开的进程
try
{
//获得需要杀死的进程名
foreach (Process thisproc in Process.GetProcessesByName(processName))
{ //立即杀死进程
thisproc.Kill();
}
}
catch (Exception Exc)
{
throw new Exception("", Exc);
}
}
#endregion

上一篇:FMDB的简单实用


下一篇:ADO.net 扩展属性