问题描述
问题是这样的。我从数据库里查询到几条数据,返回一个datatable。通过循行,生成一个<table></table>的html格式的字符串。然后通过下面:System.Web.HttpContextcurContext=System.Web.HttpContext.Current;StringWritersw=newStringWriter();sw.WriteLine("<htmlxmlns:x="urn:schemas-microsoft-com:office:excel">");sw.WriteLine("<head>");sw.WriteLine("<!--[ifgtemso9]>");sw.WriteLine("<xml>");sw.WriteLine("<x:ExcelWorkbook>");sw.WriteLine("<x:ExcelWorksheets>");sw.WriteLine("<x:ExcelWorksheet>");sw.WriteLine("<x:Name>数据报表</x:Name>");sw.WriteLine("<x:WorksheetOptions>");sw.WriteLine("<x:Print>");sw.WriteLine("<x:ValidPrinterInfo/>");sw.WriteLine("</x:Print>");sw.WriteLine("</x:WorksheetOptions>");sw.WriteLine("</x:ExcelWorksheet>");sw.WriteLine("</x:ExcelWorksheets>");sw.WriteLine("</x:ExcelWorkbook>");sw.WriteLine("</xml>");sw.WriteLine("<![endif]-->");sw.WriteLine("</head>");sw.WriteLine("<body>");sw.WriteLine(sb.ToString());sw.WriteLine("</body>");sw.WriteLine("</html>");HttpContext.Current.Response.Clear();HttpContext.Current.Response.Buffer=true;HttpContext.Current.Response.AppendHeader("content-disposition","attachment;filename="+HttpUtility.UrlEncode(fileName,Encoding.UTF8).ToString()+".xls");curContext.Response.Write("<metahttp-equiv=Content-Typecontent=text/html;charset=gb2312>");curContext.Response.ContentEncoding=System.Text.Encoding.GetEncoding("gb2312");//返回客户端curContext.Response.Write(sw);sw.Close();sb就是上面拼接的字符串变量。现在想实现一条数据生成一个sheet,就是一个excel包含多个sheet。该如何在此基础上修改?
解决方案
解决方案二:
其实就是导出excel的一个问题。初学者,求指导。详细点更好!
解决方案三:
没有人吗?帮忙顶下
解决方案四:
同求解决方法,我在<x:ExcelWorksheets>中放置多个<x:ExcelWorksheet>,能生成多个sheet页,但如何在其他sheet中放入内容呢。
解决方案五:
参考
解决方案六:
用第三方org.in2bits.MyXls非常方便,不需要安装office
解决方案七:
完全符合你的要求
解决方案八:
引用楼主sai_shuang的回复:
现在想实现一条数据生成一个sheet,就是一个excel包含多个sheet。该如何在此基础上修改?
无法在此基础上修改。这个东西根本就只能弄个最简单的table,没有任何多一点的Excel格式、公式、单元格样式等技术,更没有WorkbookSheet概念。要想输出excel,你就应该使用真正面对excel的编程方式。而不是这种。
解决方案九:
WorkbookSheet-->Worksheet生成xml格式当然是可以得到复杂的excel工作簿的。但是你花时间掌握了excelxml了吗?从csdn上无法得到好的答案。而如果你要对excel编程,最好调用封装好的excelCOM编程api。这就好像你编写c#程序,而不是直接写0、1、0、1代码一样,对于excel编程优先使用api接口库去操作,而不是直接编辑xml。
解决方案十:
http://blog.csdn.net/mh942408056/article/details/7297572?reload
解决方案十一:
openxml,aspose,epplus,npoi,com组建欢迎你
解决方案十二:
Excel文件创建多个工作表privatevoid打开Excel文件ToolStripMenuItem_Click(objectsender,EventArgse){OpenFileDialog.Filter="Excel文件|*.xls";//设置打开文件筛选器OpenFileDialog.Title="打开Excel文件";//设置打开对话框标题OpenFileDialog.Multiselect=false;//设置打开对话框中不能多选if(OpenFileDialog.ShowDialog()==DialogResult.OK)//判断是否选择了文件{M_str_Name=OpenFileDialog.FileName;//记录选择的Excel文件WBrowser_Excel.Navigate(M_str_Name);//在窗体中显示Excel文件内容}}privatevoid创建工作表ToolStripMenuItem_Click(objectsender,EventArgse){CloseProcess();//关闭Excel进程Microsoft.Office.Interop.Excel.Applicationexcel=newMicrosoft.Office.Interop.Excel.Application();//实例化Excel对象objectmissing=Missing.Value;//获取缺少的object类型值//打开指定的Excel文件Microsoft.Office.Interop.Excel.Workbookworkbook=excel.Application.Workbooks.Open(M_str_Name,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing);Microsoft.Office.Interop.Excel.WorksheetnewWorksheet=(Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Add(missing,missing,1,missing);MessageBox.Show("添加工作表成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);excel.Application.DisplayAlerts=false;//不显示提示对话框workbook.Save();//保存工作表workbook.Close(false,missing,missing);//关闭工作表WBrowser_Excel.Navigate(M_str_Name);//在窗体中显示Excel文件内容}
解决方案十三:
npoi这个很好用,怎么定义都可以,我也刚做了这个导出多个SHEET的
解决方案十四:
NPOI强烈推荐。