我的导出代码是这样的。
public static void GetDataFile(Table table)
{
//文件名为当前日期+4位随机数
string strFileName = DateTime.Today.ToString("yyyyMMdd") + new Random(DateTime.Now.Millisecond).Next(10000).ToString() + ".xls";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "Default";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + strFileName);
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("IBM437");//设置输出流为美国英语
HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.Globalization.CultureInfo ciExcel = new System.Globalization.CultureInfo("en-US", true);
StringWriter swExcel = new StringWriter(ciExcel);
System.Web.UI.HtmlTextWriter htwExcel = new System.Web.UI.HtmlTextWriter(swExcel);
table.RenderControl(htwExcel);
HttpContext.Current.Response.Write(swExcel.ToString());
HttpContext.Current.Response.End();
}希望导出的文件里就已经在指定的地方加上分页符了。
public static void GetDataFile(Table table)
{
//文件名为当前日期+4位随机数
string strFileName = DateTime.Today.ToString("yyyyMMdd") + new Random(DateTime.Now.Millisecond).Next(10000).ToString() + ".xls";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "Default";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + strFileName);
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("IBM437");//设置输出流为美国英语
HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.Globalization.CultureInfo ciExcel = new System.Globalization.CultureInfo("en-US", true);
StringWriter swExcel = new StringWriter(ciExcel);
System.Web.UI.HtmlTextWriter htwExcel = new System.Web.UI.HtmlTextWriter(swExcel);
table.RenderControl(htwExcel);
HttpContext.Current.Response.Write(swExcel.ToString());
HttpContext.Current.Response.End();
}希望导出的文件里就已经在指定的地方加上分页符了。
1. 首先必须使用下面这个html标记表示该文件是excel可识别的html内容(可以将这行作为输出的html标记)
<html xmlns:x="urn:schemas-microsoft-com:office:excel">
2. 在输出流中添加上下面的xml标记(这个标记用于控制打印或打印预览时的分行,注意下面标xml中的两个数字1和3,表示第一行和第三行后开始新的页。如果需要在第5行后又开始分页则可以在xml相应的位置添加上这样一个xml标记 '<x:RowBreak><x:Row>5</x:Row></x:RowBreak>')
<xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>a</x:Name>
<x:WorksheetOptions>
<x:Panes>
<x:Pane>
</x:Pane>
</x:Panes>
</x:WorksheetOptions>
<x:PageBreaks>
<x:RowBreaks>
<x:RowBreak>
<x:Row>1</x:Row>
</x:RowBreak>
<x:RowBreak>
<x:Row>3</x:Row>
</x:RowBreak>
</x:RowBreaks>
</x:PageBreaks>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
</x:ExcelWorkbook></xml>
3. 注意上面xml标记的位置,必须放在内容表格之前,最好放在<header>里面。另外,将这段xml实现定义好,通过xmldom对象可以很容易操作在需要分行的地方字节插入一个<x:RowBreak>元就可以了
<html xmlns:x="urn:schemas-microsoft-com:office:excel">
<xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>a</x:Name>
<x:WorksheetOptions>
<x:Panes>
<x:Pane>
</x:Pane>
</x:Panes>
</x:WorksheetOptions>
<x:PageBreaks>
<x:RowBreaks>
<x:RowBreak>
<x:Row>1</x:Row>
</x:RowBreak>
<x:RowBreak>
<x:Row>3</x:Row>
</x:RowBreak>
</x:RowBreaks>
</x:PageBreaks>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
</x:ExcelWorkbook></xml>
<table><tr>
<td>Col1,Col2,Col3</td></tr>
<tr style="page-break-before:always;"><td>A,B,C</td></tr>
<tr><td>AA,BB,CC</td></tr>
</table>
</html>