Excel可以用XML格式来实现,哪么你需要做的只是将Xml实现即可
下载疯狂代码,生成excel看看格式,然后自己构建即可
也可以新建一个excel,然后保存为xml格式即可,然后参考生成的代码格式来输出
给一个例程吧
System.Text.StringBuilder sb = new System.Text.StringBuilder(1024);
#region 头文件
sb.Append("<?xml version=\"1.0\"?>\n");
sb.Append("<?mso-application progid=\"Excel.Sheet\"?>\n");
sb.Append("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\n");
sb.Append(" xmlns:o=\"urn:schemas-microsoft-com:office:office\"\n");
sb.Append(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"\n");
sb.Append(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"\n");
sb.Append(" xmlns:html=\"
http://www.w3.org/TR/REC-html40\">\n");
sb.Append(" <DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">\n");
sb.Append(" <Author>CrazyCoder 乔松涛</Author>\n");
sb.Append(" <LastAuthor>CrazyCoder 乔松涛</LastAuthor>\n");
sb.Append(" <Created>2008-5-8 13:07:24</Created>\n");
sb.Append(" <Company>crazycoder.cn</Company>\n");
sb.Append(" <Version>11.5606</Version>\n");
sb.Append(" </DocumentProperties>\n");
sb.Append(" <ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">\n");
sb.Append(" <WindowHeight>9090</WindowHeight>\n");
sb.Append(" <WindowWidth>10380</WindowWidth>\n");
sb.Append(" <WindowTopX>360</WindowTopX>\n");
sb.Append(" <WindowTopY>45</WindowTopY>\n");
sb.Append(" <ProtectStructure>False</ProtectStructure>\n");
sb.Append(" <ProtectWindows>False</ProtectWindows>\n");
sb.Append(" </ExcelWorkbook>\n");
sb.Append(" <Styles>\n");
sb.Append(" <Style ss:ID=\"Default\" ss:Name=\"Normal\">\n");
sb.Append(" <Alignment ss:Vertical=\"Center\"/>\n");
sb.Append(" <Borders/>\n");
sb.Append(" <Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Size=\"12\"/>\n");
sb.Append(" <Interior/>\n");
sb.Append(" <NumberFormat/>\n");
sb.Append(" <Protection/>\n");
sb.Append(" </Style>\n");
sb.Append(" </Styles>\n");
sb.Append(" <Worksheet ss:Name=\"Sheet1\">\n");
sb.Append(" <Table ss:ExpandedColumnCount=\"33\" ss:ExpandedRowCount=\"11\" x:FullColumns=\"1\"\n");
sb.Append(" x:FullRows=\"1\" ss:DefaultColumnWidth=\"54\" ss:DefaultRowHeight=\"14.25\">\n");
#endregion
sb.Append(" <Row>\n");
#region 标题
//分类ID
sb.Append(" <Cell><Data ss:Type=\"String\">分类ID</Data></Cell>\n");
//用户名
sb.Append(" <Cell><Data ss:Type=\"String\">用户名</Data></Cell>\n");
//分类名
sb.Append(" <Cell><Data ss:Type=\"String\">分类名</Data></Cell>\n");
//分类封面
sb.Append(" <Cell><Data ss:Type=\"String\">分类封面</Data></Cell>\n");
//排序,倒叙排列
sb.Append(" <Cell><Data ss:Type=\"String\">排序,倒叙排列</Data></Cell>\n");
//是否私密分类
sb.Append(" <Cell><Data ss:Type=\"String\">是否私密分类</Data></Cell>\n");
//分类密码
sb.Append(" <Cell><Data ss:Type=\"String\">分类密码</Data></Cell>\n");
//相册数
sb.Append(" <Cell><Data ss:Type=\"String\">相册数</Data></Cell>\n");
//添加日期
sb.Append(" <Cell><Data ss:Type=\"String\">添加日期</Data></Cell>\n");
#endregion
sb.Append(" </Row>\n");
foreach (CategoryInfo obj in this)
{
sb.Append(" <Row>\n");
#region 内容
//分类ID
sb.Append(" <Cell><Data ss:Type=\"Number\">");
sb.Append(obj.CategoryID);
sb.Append("</Data></Cell>\n");
//用户名
sb.Append(" <Cell><Data ss:Type=\"String\">");
sb.Append(string.IsNullOrEmpty(obj.UserName) ? "" : obj.UserName.Replace(">", ">").Replace("<", "<"));
sb.Append("</Data></Cell>\n");
//分类名
sb.Append(" <Cell><Data ss:Type=\"String\">");
sb.Append(string.IsNullOrEmpty(obj.CategorName) ? "" : obj.CategorName.Replace(">", ">").Replace("<", "<"));
sb.Append("</Data></Cell>\n");
//分类封面
sb.Append(" <Cell><Data ss:Type=\"String\">");
sb.Append(string.IsNullOrEmpty(obj.Cover) ? "" : obj.Cover.Replace(">", ">").Replace("<", "<"));
sb.Append("</Data></Cell>\n");
//排序,倒叙排列
sb.Append(" <Cell><Data ss:Type=\"Number\">");
sb.Append(obj.SortID);
sb.Append("</Data></Cell>\n");
//是否私密分类
sb.Append(" <Cell><Data ss:Type=\"String\">");
sb.Append(obj.IsSecret);
sb.Append("</Data></Cell>\n");
//分类密码
sb.Append(" <Cell><Data ss:Type=\"String\">");
sb.Append(string.IsNullOrEmpty(obj.Password) ? "" : obj.Password.Replace(">", ">").Replace("<", "<"));
sb.Append("</Data></Cell>\n");
//相册数
sb.Append(" <Cell><Data ss:Type=\"Number\">");
sb.Append(obj.ArticleNum);
sb.Append("</Data></Cell>\n");
//添加日期
sb.Append(" <Cell><Data ss:Type=\"String\">");
sb.Append(obj.AddDate);
sb.Append("</Data></Cell>\n");
#endregion
sb.Append(" </Row>\n");
}
sb.Append(" </Table>\n");
#region 尾文件
sb.Append(" <WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">\n");
sb.Append(" <Selected/>\n");
sb.Append(" <Panes>\n");
sb.Append(" <Pane>\n");
sb.Append(" <Number>3</Number>\n");
sb.Append(" <ActiveRow>9</ActiveRow>\n");
sb.Append(" <ActiveCol>10</ActiveCol>\n");
sb.Append(" </Pane>\n");
sb.Append(" </Panes>\n");
sb.Append(" <ProtectObjects>False</ProtectObjects>\n");
sb.Append(" <ProtectScenarios>False</ProtectScenarios>\n");
sb.Append(" </WorksheetOptions>\n");
sb.Append(" </Worksheet>\n");
sb.Append("</Workbook>\n");
#endregion
return sb.ToString();
参考资料:http://www.crazycoder.cn/