winform从数据库导出成excel
有textbox输入要导出多少条数据,输出的数据update改变状态,然后要有页眉页脚,还有个控制字体的...应该有很多人写过这种程序吧.
谁分享一下思路,意见,代码,谢谢

解决方案 »

  1.   

    导出Excel做过
    页眉页脚没搞成
      

  2.   

    用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 
      

  3.   

    其实思路很简单,你用记事本打开一个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();
        }
      

  4.   

    思路 就是先从数据库读出你想要的数据 然后写入进去;
    存在的问题: 效率优先1.如何快速的从数据库读出你想要的数据,尤其是那些只读取数据库一部分 而且又不小的数据。
    2.如何快速的写入到excel表里面,目前最通用的方法是range整块输入, 但是考虑到数据不同的字体颜色等设定,只有想其他更好的办法。说白了 就是数据库到一定数量级的时候,效率很大程度上就成为瓶颈。比如你读取10万数据的时间 和写10万条数据的时间 都会成为很大的难题祝你好运!
      

  5.   

    我就不信CSDN没有人做过……
    继续坐等……