将GridView的数据导入excel的时将标题居中,并将标题下的时间段放在右或居中,我的代码如下:
最好能给我直接加就能用嘿嘿!
Response.Clear();
Response.Buffer = true;
string style = @"<style> .text { mso-number-format:\@; } </script> ";
Response.ContentType = "application/ms-excel";
this.EnableViewState = true;
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
sw.WriteLine("<tr><td colspan=5>标题</td><tr>");
sw.WriteLine("<br/><tr><td colspan=5>时间</td><tr>");
GridView1.RenderControl(htw);
Response.Write(style);
Response.Write(sw.ToString());
Response.End();
想要的格式就是:
标题
时间段
GridView的数据
注:格式要整齐
最好能给我直接加就能用嘿嘿!
Response.Clear();
Response.Buffer = true;
string style = @"<style> .text { mso-number-format:\@; } </script> ";
Response.ContentType = "application/ms-excel";
this.EnableViewState = true;
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
sw.WriteLine("<tr><td colspan=5>标题</td><tr>");
sw.WriteLine("<br/><tr><td colspan=5>时间</td><tr>");
GridView1.RenderControl(htw);
Response.Write(style);
Response.Write(sw.ToString());
Response.End();
想要的格式就是:
标题
时间段
GridView的数据
注:格式要整齐
解决方案 »
- c# 怎么把数据写入excel
- 杂整呢?难道真要重装系统
- 关于类属性的问题!各位帮忙了~谢谢先了~
- 为什么我的openfiledialog每次都要出现两次?请高手们指点一下。
- 在C#编写的Client程序中如何引用服务器上的 Web service 服务 请解答
- 自己新建一个类,调用Response.write()方法老是报错,搞不定了!!!
- 关于Des加密算法解密的问题
- static关键字,到底在什么时候用在定义类、属性的什么地方??它有什么用处??
- 怎样把一个数格式成百分比形式
- C#在xp下使用wmi正常,但98下出错,谁能解决?
- 不显示删除回复显示所有回复显示星级回复显示得分回复 ReportViewer + datasetxsd 数据集 + char图表做报表的怪事 那们大侠帮帮忙
- 急!Gridview控件如何根据某个字段值的不同,将相应行显示或不显示,同时也可以分页
给你一个excel处理方法
其中
Excel.Range range = sheet.get_Range(sheet.Cells[x1, y1], sheet.Cells[x2, y2]);
range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter; public void SetHeadContent(int x1, int y1, int x2, int y2, System.Drawing.Font font, System.Drawing.Color color, string text)
{
//Excel.Range range = sheet.get_Range("A1", "A8");
//Excel.Range range = sheet.get_Range(this.GetAix(x1, y1), this.GetAix(x2, y2));
Excel.Range range = sheet.get_Range(sheet.Cells[x1, y1], sheet.Cells[x2, y2]);
range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
range.Merge(true);
range.set_Value(miss, text);
if (font != null)
{
range.Font.Size = font.Size;
range.Font.Bold = font.Bold;
range.Font.Name = font.Name;
range.Font.Italic = font.Italic;
range.Font.Underline = font.Underline;
}
if (color != null)
{
range.Font.Color = System.Drawing.ColorTranslator.ToOle(color);
}
}