winform从数据库导出成excel,页居页脚 winform从数据库导出成excel有textbox输入要导出多少条数据,输出的数据update改变状态,然后要有页眉页脚,还有个控制字体的...应该有很多人写过这种程序吧.谁分享一下思路,意见,代码,谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 导出Excel做过页眉页脚没搞成 用VB写把 - - 对于EXCEL的操作 VB还快点。。Sub SetPrint() With ActiveSheet.PageSetup .PrintTitleRows = "$1:$2" '顶端打印标题行区域 .LeftHeader = "" '设置左边页眉显示 .CenterHeader = "" '设置居中页眉显示 .RightHeader = "" '设置右边页眉显示 .LeftFooter = "" '设置左边页脚显示 .CenterFooter = "第 &P 页,共 &N 页" '设置居中页脚显示 .RightFooter = "" '设置右边页脚显示 .LeftMargin = 1 * 567 '设置左边距为1厘米 .RightMargin = 1.5 * 567 '设置右边距为1.5厘米 .TopMargin = 2.2 * 567 '设置上边距为2.2厘米 .BottomMargin = 2.1 * 567 '设置下边距为2.1厘米 .PaperSize = xlPaperA4 '设置纸张大小 End With End Sub Sub SetFont(DanYuanGe As String) Range(DanYuanGe).Select With Selection.Font .Name = "黑体" '设置字体 .Size = 16 '设置字号 End With With Selection .HorizontalAlignment = xlCenter '设置水平对齐方式为居中 .VerticalAlignment = xlTop '设置垂直对齐方式为靠上 End With End Sub 其实思路很简单,你用记事本打开一个excel文件,会发现excel只不过是个xml文件而已,用程序构造这个xml并添加上样式即可。例子:StringBuilder ListStr = new StringBuilder("<?xml version=\"1.0\"?>" + "<?mso-application progid=\"Excel.Sheet\"?>" + "<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" " + "xmlns:o=\"urn:schemas-microsoft-com:office:office\" " + "xmlns:x=\"urn:schemas-microsoft-com:office:excel\" " + "xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" " + "xmlns:html=\"http://www.w3.org/TR/REC-html40\">" + "<Styles> <Style ss:ID=\"Default\" ss:Name=\"Normal\">" + "<Alignment ss:Vertical=\"Center\"/>" + "<Borders/>" + "<Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Size=\"12\"/>" + "<Interior/>" + "<NumberFormat/>" + "<Protection/>" + "</Style>" + "<Style ss:ID=\"s22\">" + "<Alignment ss:Horizontal=\"Left\" ss:Vertical=\"Center\"/>" + "<Borders>" + "<Border ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>" + "<Border ss:Position=\"Left\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>" + "<Border ss:Position=\"Right\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>" + "<Border ss:Position=\"Top\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>" + "</Borders>" + "<Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Color=\"#333399\" ss:Bold=\"1\"/>" + "<Interior ss:Color=\"#FFCC99\" ss:Pattern=\"Solid\"/>" + "</Style>" + "</Styles><Worksheet ss:Name=\"Sheet1\">"); ListStr.Append(GetExcelTable("这里放你的dataTable")); ListStr.Append("</Worksheet></Workbook>");private static string GetExcelTable(DataTable ExcelDataTable) { StringBuilder ListStr = new StringBuilder(""); DataTable DataIn = ExcelDataTable; ListStr.Append("<Table><Row>"); if (DataIn != null && DataIn.Rows.Count > 0) { int ColCount = DataIn.Columns.Count; int RowCount = DataIn.Rows.Count; for (int i = 0; i < ColCount; i++) { //标题列 ListStr.Append("<Cell ss:StyleID=\"s22\"><Data ss:Type=\"String\">" + HttpUtility.HtmlEncode(DataIn.Columns[i].ColumnName) + "</Data></Cell>"); } ListStr.Append("</Row>"); string TmpValue = ""; for (int i = 0; i < RowCount; i++) { ListStr.Append("<Row>"); for (int j = 0; j < ColCount; j++) { //数据列 TmpValue = ""; TmpValue = DataIn.Rows[i][j].ToString().Trim(); ListStr.Append("<Cell><Data ss:Type=\"String\">" + HttpUtility.HtmlEncode(TmpValue) + "</Data></Cell>"); } ListStr.Append("</Row>"); } } else { ListStr.Append("<Cell><Data ss:Type=\"String\">" + LangHelper.WordGet("检索不到数据,请重新选择检索条件") + "</Data></Cell></Row>"); } ListStr.Append("</Table>"); return ListStr.ToString(); } 思路 就是先从数据库读出你想要的数据 然后写入进去;存在的问题: 效率优先1.如何快速的从数据库读出你想要的数据,尤其是那些只读取数据库一部分 而且又不小的数据。2.如何快速的写入到excel表里面,目前最通用的方法是range整块输入, 但是考虑到数据不同的字体颜色等设定,只有想其他更好的办法。说白了 就是数据库到一定数量级的时候,效率很大程度上就成为瓶颈。比如你读取10万数据的时间 和写10万条数据的时间 都会成为很大的难题祝你好运! 我就不信CSDN没有人做过……继续坐等…… 求一正则 .net 网站 .aspx页面中的 javascript怎么缩进 如何实现带链接listbox DBF文件名不能大于8位 关于picturebox用按纽绘图的问题,急救。。。。。 如何实现像windows任务栏一样停靠后不会覆盖桌面 c如何构建多(模块)dll程序 推荐:c#作的短信开发平台 还有源代码 目录路径问题 简单问题? GDI基础问题 请教如何分开提取数值
页眉页脚没搞成
With ActiveSheet.PageSetup
.PrintTitleRows = "$1:$2" '顶端打印标题行区域
.LeftHeader = "" '设置左边页眉显示
.CenterHeader = "" '设置居中页眉显示
.RightHeader = "" '设置右边页眉显示
.LeftFooter = "" '设置左边页脚显示
.CenterFooter = "第 &P 页,共 &N 页" '设置居中页脚显示
.RightFooter = "" '设置右边页脚显示
.LeftMargin = 1 * 567 '设置左边距为1厘米
.RightMargin = 1.5 * 567 '设置右边距为1.5厘米
.TopMargin = 2.2 * 567 '设置上边距为2.2厘米
.BottomMargin = 2.1 * 567 '设置下边距为2.1厘米
.PaperSize = xlPaperA4 '设置纸张大小
End With
End Sub Sub SetFont(DanYuanGe As String)
Range(DanYuanGe).Select
With Selection.Font
.Name = "黑体" '设置字体
.Size = 16 '设置字号
End With
With Selection
.HorizontalAlignment = xlCenter '设置水平对齐方式为居中
.VerticalAlignment = xlTop '设置垂直对齐方式为靠上
End With
End Sub
用程序构造这个xml并添加上样式即可。例子:
StringBuilder ListStr = new StringBuilder("<?xml version=\"1.0\"?>" +
"<?mso-application progid=\"Excel.Sheet\"?>" +
"<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" " +
"xmlns:o=\"urn:schemas-microsoft-com:office:office\" " +
"xmlns:x=\"urn:schemas-microsoft-com:office:excel\" " +
"xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" " +
"xmlns:html=\"http://www.w3.org/TR/REC-html40\">" +
"<Styles> <Style ss:ID=\"Default\" ss:Name=\"Normal\">" +
"<Alignment ss:Vertical=\"Center\"/>" +
"<Borders/>" +
"<Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Size=\"12\"/>" +
"<Interior/>" +
"<NumberFormat/>" +
"<Protection/>" +
"</Style>" +
"<Style ss:ID=\"s22\">" +
"<Alignment ss:Horizontal=\"Left\" ss:Vertical=\"Center\"/>" +
"<Borders>" +
"<Border ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>" +
"<Border ss:Position=\"Left\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>" +
"<Border ss:Position=\"Right\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>" +
"<Border ss:Position=\"Top\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>" +
"</Borders>" +
"<Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Color=\"#333399\" ss:Bold=\"1\"/>" +
"<Interior ss:Color=\"#FFCC99\" ss:Pattern=\"Solid\"/>" +
"</Style>" +
"</Styles><Worksheet ss:Name=\"Sheet1\">"); ListStr.Append(GetExcelTable("这里放你的dataTable"));
ListStr.Append("</Worksheet></Workbook>");private static string GetExcelTable(DataTable ExcelDataTable)
{
StringBuilder ListStr = new StringBuilder("");
DataTable DataIn = ExcelDataTable;
ListStr.Append("<Table><Row>");
if (DataIn != null && DataIn.Rows.Count > 0)
{
int ColCount = DataIn.Columns.Count;
int RowCount = DataIn.Rows.Count;
for (int i = 0; i < ColCount; i++)
{
//标题列
ListStr.Append("<Cell ss:StyleID=\"s22\"><Data ss:Type=\"String\">" + HttpUtility.HtmlEncode(DataIn.Columns[i].ColumnName) + "</Data></Cell>");
}
ListStr.Append("</Row>");
string TmpValue = "";
for (int i = 0; i < RowCount; i++)
{
ListStr.Append("<Row>");
for (int j = 0; j < ColCount; j++)
{
//数据列
TmpValue = "";
TmpValue = DataIn.Rows[i][j].ToString().Trim();
ListStr.Append("<Cell><Data ss:Type=\"String\">" + HttpUtility.HtmlEncode(TmpValue) + "</Data></Cell>");
}
ListStr.Append("</Row>"); }
}
else
{
ListStr.Append("<Cell><Data ss:Type=\"String\">" + LangHelper.WordGet("检索不到数据,请重新选择检索条件") + "</Data></Cell></Row>");
}
ListStr.Append("</Table>"); return ListStr.ToString();
}
存在的问题: 效率优先1.如何快速的从数据库读出你想要的数据,尤其是那些只读取数据库一部分 而且又不小的数据。
2.如何快速的写入到excel表里面,目前最通用的方法是range整块输入, 但是考虑到数据不同的字体颜色等设定,只有想其他更好的办法。说白了 就是数据库到一定数量级的时候,效率很大程度上就成为瓶颈。比如你读取10万数据的时间 和写10万条数据的时间 都会成为很大的难题祝你好运!
继续坐等……