一、利用POI生成Excel强制换行
1. 首先在需要强制换行的单元格里使用poi的样式,并且把样式设定为自动换行
# HSSFCellStyle cellStyle=workbook.createCellStyle();
# cellStyle.setWrapText(true);
# cell.setCellStyle(cellStyle);
2. 其次是在需要强制换行的单元格,使用\就可以实再强制换行
1. HSSFCell cell = row.createCell((short)0);
2. cell.setCellStyle(cellStyle); cell.setCellValue(new HSSFRichTextString("hello\r\n world!"));
这样就能实现强制换行,
换行后的效里是单元格里强制换行
hello
world!
二、流数据
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GridView1.DataSource = CreateDataSource();
this.GridView1.DataBind();
}
}
public override void VerifyRenderingInServerForm(Control control)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
//设置文件名
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss");
//HTTP请求内容
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
// 输出文件流
System.IO.StringWriter strWriter = null;
//服务端控件输出流
System.Web.UI.HtmlTextWriter htmlWriter = null;
// 清空当前 Response
HttpContext.Current.Response.Clear();
//设置缓冲方式输出
HttpContext.Current.Response.Buffer = true;
//添加HTTP标头到输出流
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.xls", fileName));
curContext.Response.ContentType = "application/vnd.ms-excel"; //设置输出文件类型为xls文件
/**如果要输出doc文件,请用下面这段代码**/
//HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.doc", fileName));
//curContext.Response.ContentType = "application/ms-word"; //设置输出文件类型为word文件
//设置编码方式
curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
curContext.Response.Charset = "";
// 导出文件
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
htmlWriter.Write("测试标题1");
// 返回客户端
GridView1.RenderControl(htmlWriter);
//设置换行 <br/>是html的换行样式替换成<br style='mso-data-placement:same-cell;'/> EXCEL换行
curContext.Response.Write(strWriter.ToString().Replace("<br/>", "<br style='mso-data-placement:same-cell;'/> "));
curContext.Response.End();
}
System.Data.DataView CreateDataSource()
{
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("测试1"));
dt.Columns.Add(new System.Data.DataColumn("测试2"));
dt.Columns.Add(new System.Data.DataColumn("测试3"));
dt.Columns.Add(new System.Data.DataColumn("测试4"));
dt.Columns.Add(new System.Data.DataColumn("测试5"));
dt.Columns.Add(new System.Data.DataColumn("测试6"));
for (int i = 0; i < 5; i++)
{
System.Random rd = new System.Random(Environment.TickCount * i); ;
dr = dt.NewRow();
dr[0] = "编号" + i.ToString();
dr[1] = "换行测试:" + i.ToString() + "<br/>" + "第二行开始" + i.ToString() + "<br/>" + "第三行开始" + i.ToString() + "<br/>" + "第四行开始" + i.ToString() + "<br/>" + "第五行开始" + i.ToString();
dr[2] = "测试:" + i.ToString();
dr[3] = "测试:" + i.ToString();
dr[4] = "测试:" + i.ToString();
dr[5] = "测试:" + i.ToString();
dt.Rows.Add(dr);
}
System.Data.DataView dv = new System.Data.DataView(dt);
return dv;
}
}
三、MYSQL查询列名和表名
select column_name from information_schema.columns where table_name='helloworld';
select table_name from information_schema.tables where table_schema='auo'
四、HtmlEncode,DataFormatString(数据对应列是时间格式才有效)
<asp:BoundField DataField="Test" HeaderText="Test" HtmlEncode="false" /> <asp:BoundField DataField="TestDate" HeaderText="TestDate" HtmlEncode="false" DataFormatString="{0:yyyy年M月d日}" /> <asp:BoundField DataField="TestDate" HeaderText="TestDate" HtmlEncode="false" DataFormatString="{0:yyyy年M月d日 HH时mm分ss秒}" />