导出到Excel功能不起作用

发布于 2024-11-06 05:05:17 字数 1345 浏览 3 评论 0原文

Somsone 建议我使用此函数将数据表导出到 Excel,但它导出的是 HTML,而不仅仅是表中的数据。如何让它仅导出数据和格式(宽度、颜色...)?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
function ExportHTMLTableToExcel()
{
   var thisTable = document.getElementById("table").innerHTML;
   window.clipboardData.setData("Text", thisTable);
   var objExcel = new ActiveXObject ("Excel.Application");
   objExcel.visible = true;

   var objWorkbook = objExcel.Workbooks.Add;
   var objWorksheet = objWorkbook.Worksheets(1);
   objWorksheet.Paste;
   alert('test');

}
</script>
  <title>Java Friends</title>
</head>
<body>
  <table id="table" style="font-weight: bold">
    <tr style="background-color:red"><td>a</td><td>b</td></tr>
    <tr><td>1</td><td>2</td></tr>
    <tr>
      <td colspan="2">
        <button onclick="ExportHTMLTableToExcel()">
            Get as Excel spreadsheet
        </button>
      </td>
    </tr>
  </table>    
</body>
</html>

注意:此功能仅在安全选项下载未签名的 ActiveX 控件下载签名的 ActiveX 控件 设置为“启用”时在 IE 中起作用

Somsone have recommanded i use this function to export my data table to excel but it exports the HTML not just the data in the table. How can I make it export the data and the formatting(width, colour...) only?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
function ExportHTMLTableToExcel()
{
   var thisTable = document.getElementById("table").innerHTML;
   window.clipboardData.setData("Text", thisTable);
   var objExcel = new ActiveXObject ("Excel.Application");
   objExcel.visible = true;

   var objWorkbook = objExcel.Workbooks.Add;
   var objWorksheet = objWorkbook.Worksheets(1);
   objWorksheet.Paste;
   alert('test');

}
</script>
  <title>Java Friends</title>
</head>
<body>
  <table id="table" style="font-weight: bold">
    <tr style="background-color:red"><td>a</td><td>b</td></tr>
    <tr><td>1</td><td>2</td></tr>
    <tr>
      <td colspan="2">
        <button onclick="ExportHTMLTableToExcel()">
            Get as Excel spreadsheet
        </button>
      </td>
    </tr>
  </table>    
</body>
</html>

Note: this function only works in IE if the security options download unsigned activex control and download signed activex control are set to 'enable'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清晰传感 2024-11-13 05:05:17

请使用此代码,它对我来说工作正常

public void ExportToSpreadsheet(DataTable table, string name)
        {
            try
            {
                HttpContext context = HttpContext.Current;
                context.Response.Clear();
                context.Response.ClearHeaders();
                String sr = string.Empty;
                sr += "<html><body><table>";
                sr += "<tr style=\"background-color:gray;color:white;\">";
                foreach (DataColumn column in table.Columns)
                {
                    sr += "<th>";
                    sr += column.ColumnName;
                    sr += "</th>";
                }
                sr += "</tr>";
                sr += Environment.NewLine;
                foreach (DataRow row in table.Rows)
                {
                    sr += "<tr>";
                    for (int i = 0; i < table.Columns.Count; i++)
                    {
                        sr += "<td>";
                        sr += row.ItemArray[i];
                        sr += "</td>";
                    }
                    sr += "</tr>";
                    sr += Environment.NewLine;
                }
                sr += "</table></body></html>";               
                context.Response.ContentType = "application/vnd.ms-excel";               
                context.Response.AddHeader("Content-Disposition", "attachment; filename=" + name + ".xls");                        
                context.Response.Write(sr);
                context.Response.Flush();
                context.Response.End();
                context.Response.Close();
            }
            catch (Exception ex)
            {

            }
        }

希望这有帮助。

Please use this code, it is working fine for me

public void ExportToSpreadsheet(DataTable table, string name)
        {
            try
            {
                HttpContext context = HttpContext.Current;
                context.Response.Clear();
                context.Response.ClearHeaders();
                String sr = string.Empty;
                sr += "<html><body><table>";
                sr += "<tr style=\"background-color:gray;color:white;\">";
                foreach (DataColumn column in table.Columns)
                {
                    sr += "<th>";
                    sr += column.ColumnName;
                    sr += "</th>";
                }
                sr += "</tr>";
                sr += Environment.NewLine;
                foreach (DataRow row in table.Rows)
                {
                    sr += "<tr>";
                    for (int i = 0; i < table.Columns.Count; i++)
                    {
                        sr += "<td>";
                        sr += row.ItemArray[i];
                        sr += "</td>";
                    }
                    sr += "</tr>";
                    sr += Environment.NewLine;
                }
                sr += "</table></body></html>";               
                context.Response.ContentType = "application/vnd.ms-excel";               
                context.Response.AddHeader("Content-Disposition", "attachment; filename=" + name + ".xls");                        
                context.Response.Write(sr);
                context.Response.Flush();
                context.Response.End();
                context.Response.Close();
            }
            catch (Exception ex)
            {

            }
        }

Hope this help.

喜爱纠缠 2024-11-13 05:05:17

jQuery DataTables TableTools 插件可让您轻松地将 HTML 表格导出到 Excel,但它不会处理格式设置。

您可以在此处查看运行插件的示例: http://www.datatables.net/extras /tabletools/

它可能无法处理格式,但它确实使导出 HTML 表格变得非常容易。

如果绝对需要格式化,那么您需要考虑将数据发送回服务器,然后让服务器将 HTML 处理为 Excel。如果您使用 ASP.net,那么这实际上很容易做到。

The jQuery DataTables TableTools plug-in will let you very easily export an HTML table to Excel but it won't handle the formatting.

You can see an example of the plug-in running here: http://www.datatables.net/extras/tabletools/

It may not handle formatting but it does make exporting the HTML table super easy.

If formatting is absolutely required then you'll need to look into sending the data back to the server and then having the server process the HTML into Excel. If you're using ASP.net then it is actually quite easy to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文