62,052
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 将Web控件导出
/// </summary>
/// <param name="source">控件实例</param>
/// <param name="type">类型:Excel或Word</param>
public void ExpertControl(System.Web.UI.Control source, DocumentType type)
{
//设置Http的头信息,编码格式
if (type == DocumentType.Excel)
{
//Excel
Response.AppendHeader("Content-Disposition","attachment;filename=result.xls");
Response.ContentType = "application/ms-excel";
}
else if (type == DocumentType.Word)
{
//Word
Response.AppendHeader("Content-Disposition","attachment;filename=result.doc");
Response.ContentType = "application/ms-word";
}
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.UTF8;
//关闭控件的视图状态
source.Page.EnableViewState =false;
//初始化HtmlWriter
System.IO.StringWriter writer = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
source.RenderControl(htmlWriter);
//输出
Response.Write(writer.ToString());
Response.End();
}
//文档类型
public enum DocumentType
{
Word,
Excel
}
/// <summary>
/// 把Gridview中数据导入到Excel的类
/// </summary>
public class GridViewToExcel
{
public GridViewToExcel()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/**//// <summary>
/// /// 把Gridview中数据导入到Excel中
/// </summary>
/// <param name="gv">需要导出数据的Gridview</param>
/// <param name="ds">Gridview的数据源</param>
/// <param name="strFileName">默认的导出Excel的文件名</param>
/// <param name="bolPart">全部还是部分导出到Excel.部分:true. 全部:false</param>
public static void ConvertToExcel(GridView gv, DataSet ds, string strFileName, bool bolPart)
{
gv.AllowPaging = bolPart;//设置导出数据是全部还是部分
gv.DataSource = ds;
gv.DataBind();
for (int i = 0; i < gv.Columns.Count; i++) //设置每个单元格
{
gv.Columns[i].ItemStyle.HorizontalAlign = HorizontalAlign.Left;
for (int j = 0; j < gv.Rows.Count; j++)
{
gv.Rows[j].Cells[i].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
}
}
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;//设置UTF8有时候出乱码
strFileName += ".xls";
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlPathEncode(strFileName));//设置默认文件名
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
//预防出现控件必须放在具有 runat=server 的窗体标记内的错误
Page page = new Page();
HtmlForm form = new HtmlForm();
gv.EnableViewState = false;
page.EnableEventValidation = false;
page.DesignerInitialize();
page.Controls.Add(form);
form.Controls.Add(gv);
page.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
/// <summary>
/// 将图片填充到Excel中的某个或某些单元格中
/// </summary>
/// <param name="ws">Microsoft.Office.Interop.Excel.Worksheet</param>
/// <param name="m_objRange"> Microsoft.Office.Interop.Excel.Range</param>
/// <param name="PicturePath">插入图片的绝对物理路径</param>
/// <param name="IsMergeCells">是否合并上面的单元格</param>
public void InsertPicture(Microsoft.Office.Interop.Excel.Worksheet ws, Microsoft.Office.Interop.Excel.Range m_objRange, string PicturePath, bool IsMergeCells)
{
//计算单元格的宽和高
float PictuteWidth, PictureHeight;
PictuteWidth = Convert.ToSingle(m_objRange.Width);
PictureHeight = Convert.ToSingle(m_objRange.Height);
PictuteWidth = 150;
PictureHeight = 200;
if (IsMergeCells)
{
//合并单元格
m_objRange.Merge(System.Reflection.Missing.Value);
}
m_objRange.Select();
float PicLeft, PicTop;
PicLeft = Convert.ToSingle(m_objRange.Left);
PicTop = Convert.ToSingle(m_objRange.Top);
ws.Shapes.AddPicture(PicturePath, Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoTrue, PicLeft+2, PicTop, PictuteWidth, PictureHeight);
}
调用
Microsoft.Office.Interop.Excel.Range rg = (Microsoft.Office.Interop.Excel.Range)ws.Cells[j + 2, k + 1];
if (dt.Columns[k].ColumnName=="Thumbnails")
{
#region 生成图片
string filePath = Application.StartupPath + "//NetPagePic//" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";
Image img= BytesToImage((byte[])dt.Rows[j][k]);
img.Save(filePath);
#endregion
InsertPicture(ws, rg, filePath, true);//向Excel插入图片
File.Delete(filePath);//删除文件
rg.RowHeight = 200;
rg.ColumnWidth =25;//宽度设置为150时在Excel中实际宽度大大超出了150。顾修改为25
}
else
{
rg.NumberFormatLocal = "@";
ws.Cells[j + 2, k + 1] = dt.Rows[j][k].ToString();
}
/// <summary>
/// 把Gridview中数据导入到Excel的类
/// </summary>
public class GridViewToExcel
{
public GridViewToExcel()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/**//// <summary>
/// /// 把Gridview中数据导入到Excel中
/// </summary>
/// <param name="gv">需要导出数据的Gridview</param>
/// <param name="ds">Gridview的数据源</param>
/// <param name="strFileName">默认的导出Excel的文件名</param>
/// <param name="bolPart">全部还是部分导出到Excel.部分:true. 全部:false</param>
public static void ConvertToExcel(GridView gv, DataSet ds, string strFileName, bool bolPart)
{
gv.AllowPaging = bolPart;//设置导出数据是全部还是部分
gv.DataSource = ds;
gv.DataBind();
for (int i = 0; i < gv.Columns.Count; i++) //设置每个单元格
{
gv.Columns[i].ItemStyle.HorizontalAlign = HorizontalAlign.Left;
for (int j = 0; j < gv.Rows.Count; j++)
{
gv.Rows[j].Cells[i].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
}
}
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;//设置UTF8有时候出乱码
strFileName += ".xls";
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlPathEncode(strFileName));//设置默认文件名
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
//预防出现控件必须放在具有 runat=server 的窗体标记内的错误
Page page = new Page();
HtmlForm form = new HtmlForm();
gv.EnableViewState = false;
page.EnableEventValidation = false;
page.DesignerInitialize();
page.Controls.Add(form);
form.Controls.Add(gv);
page.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}