求将repeater中的数据导出到Excel中的代码,只是导出数据,其他的不用导出,谁能提供一份代码?谢谢!

解决方案 »

  1.   

      
    StringBuilder strExcel = new StringBuilder();
                strExcel.Append("<%@ page contentType='application/vnd.ms-excel'%> ");
                strExcel.Append(@"<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>");
                strExcel.Append(Environment.NewLine);
                strExcel.Append("<head>");
                strExcel.Append("<!--[if gte mso 9]><xml>");
                strExcel.Append("<x:ExcelWorkbook>");
                strExcel.Append("<x:ExcelWorksheets>");
                strExcel.Append("<x:ExcelWorksheet>");
                strExcel.Append("<x:Name>Sheet1</x:Name>");
                strExcel.Append("<x:WorksheetOptions>");
                strExcel.Append("<x:DefaultRowHeight>285</x:DefaultRowHeight>");
                strExcel.Append("<x:Selected/>");
                strExcel.Append("<x:Panes>");
                strExcel.Append(" <x:Pane>");
                strExcel.Append("<x:Number>3</x:Number>");
                strExcel.Append("<x:ActiveRow>3</x:ActiveRow>");
                strExcel.Append("<x:ActiveCol>2</x:ActiveCol>");
                strExcel.Append("</x:Pane>");
                strExcel.Append("</x:Panes>");
                strExcel.Append("<x:ProtectContents>False</x:ProtectContents>");
                strExcel.Append("<x:ProtectObjects>False</x:ProtectObjects>");
                strExcel.Append("<x:ProtectScenarios>False</x:ProtectScenarios>");
                strExcel.Append("</x:WorksheetOptions>");
                strExcel.Append(" </x:ExcelWorksheet>");
                strExcel.Append("<x:ExcelWorksheet>");
                strExcel.Append(" <x:Name>Sheet2</x:Name>");
                strExcel.Append(" <x:WorksheetOptions>");
                strExcel.Append("  <x:DefaultRowHeight>285</x:DefaultRowHeight>");
                strExcel.Append("<x:ProtectContents>False</x:ProtectContents>");
                strExcel.Append("<x:ProtectObjects>False</x:ProtectObjects>");
                strExcel.Append("<x:ProtectScenarios>False</x:ProtectScenarios>");
                strExcel.Append("</x:WorksheetOptions>");
                strExcel.Append("</x:ExcelWorksheet>");
                strExcel.Append("<x:ExcelWorksheet>");
                strExcel.Append(" <x:Name>Sheet3</x:Name>");
                strExcel.Append("<x:WorksheetOptions>");
                strExcel.Append(" <x:DefaultRowHeight>285</x:DefaultRowHeight>");
                strExcel.Append(" <x:ProtectContents>False</x:ProtectContents>");
                strExcel.Append(" <x:ProtectObjects>False</x:ProtectObjects>");
                strExcel.Append(" <x:ProtectScenarios>False</x:ProtectScenarios>");
                strExcel.Append("  </x:WorksheetOptions>");
                strExcel.Append(" </x:ExcelWorksheet>");
                strExcel.Append("</x:ExcelWorksheets>");
                strExcel.Append("<x:WindowHeight>9600</x:WindowHeight>");
                strExcel.Append(" <x:WindowWidth>15075</x:WindowWidth>");
                strExcel.Append("<x:WindowTopX>480</x:WindowTopX>");
                strExcel.Append("<x:WindowTopY>90</x:WindowTopY>");
                strExcel.Append("<x:ProtectStructure>False</x:ProtectStructure>");
                strExcel.Append("<x:ProtectWindows>False</x:ProtectWindows>");
                strExcel.Append("</x:ExcelWorkbook>");
                strExcel.Append("</xml><![endif]-->");            strExcel.Append("<style type='text/css'>");
                strExcel.Append("td{");
                strExcel.Append("mso-number-format:'\\@';}");
                strExcel.Append(Environment.NewLine);
                strExcel.Append("</style>");            strExcel.Append("</head>");
                strExcel.Append("<table id='tableExcel' width='100%'  style='border-collapse:collapse'>");
                strExcel.Append(Environment.NewLine);
                strExcel.Append("<tr align='center'>");
                strExcel.Append(Environment.NewLine);
                foreach (string str in listCaption)
                {
                    strExcel.Append("<td>");
                    strExcel.Append(str);
                    strExcel.Append("</td>");
                }
                strExcel.Append(Environment.NewLine);
                strExcel.Append("</tr>");            foreach (DataRow row in dataTbale.Rows)
                {
                    strExcel.Append("<tr align='left'>");
                    foreach (string str in listFiled)
                    {
                        if (str.ToUpper() == "SZXM")
                        {
                            if (row["TJFS"].ToString() == "4")
                            {
                                List<Srxm> listInfo = row["SRXM"] as List<Srxm>;
                                string strBm = string.Empty;
                                foreach (Srxm srxm in listInfo)
                                    strBm += srxm.SRXM_BM + ";";
                                strExcel.Append("<td>");
                                strExcel.Append(strBm.TrimEnd(';'));
                                strExcel.Append("</td>");
                            }
                            else if (row["TJFS"].ToString() == "7")
                            {
                                List<Zcxm> listInfo = row["ZCXM"] as List<Zcxm>;
                                string strBm = string.Empty;
                                foreach (Zcxm zcxm in listInfo)
                                    strBm += zcxm.ZCXM_BM + ";";
                                strExcel.Append("<td>");
                                strExcel.Append(strBm.TrimEnd(';'));
                                strExcel.Append("</td>");
                            }
                        }
                        else
                        {
                            strExcel.Append("<td>");
                            strExcel.Append(row[str].ToString());
                            strExcel.Append("</td>");
                        }                }
                    strExcel.Append("</tr>");            }
                strExcel.Append(Environment.NewLine);
                strExcel.Append("</table>");
                strExcel.Append(Environment.NewLine);
                strExcel.Append("</html>");不解释了~~就是拼一个HTML,最后保存的时候,保存成XLS~~~