如何将html文件加载到Excel电子表格中

时间:2013-07-22 10:49:10

标签: excel excel-vba vba

我们定期以html文件的形式获取一组SQL查询输出。所以,基本上,这个html文件包含一组表格形式的SQL输出,我可以右键单击并在MS Excel中打开它。

我基本上是尝试在Excel中加载html并将所有输出放在不同的工作表中(每个SQL输出一个)而不是每次都右键单击。

我是Excel新手,不知道如何实现这一目标。

请指点我如何实现这个目标?

非常感谢

2 个答案:

答案 0 :(得分:0)

我建议使用Excel Web查询:请参阅http://www.techrepublic.com/article/pull-data-into-microsoft-excel-with-web-queries/或在Google上搜索excel网页查询。 您可以提供一个URL,例如file:// C:\ somefolder \ somehtmlfile.html 您可以在此处找到有关如何有问题地创建和刷新Web查询的更多信息: http://msdn.microsoft.com/en-us/library/office/aa203721(v=office.11).aspx 假设使用电子表格对相关数据库表具有读访问权限,则更好的选择是将数据直接提取到Excel中。

答案 1 :(得分:0)

如果您计划将其作为自动化,则可以使用C#。 实际上,如果您将网站上的内容复制到C#中的变量,然后从C#中复制,则将其粘贴到您选择的位置。 因此,我们使用剪贴板作为帮助,它允许将内容复制到剪贴板,实际上将任何html表转换为Excel表。

Microsoft.Office.Interop.Excel.Application excel = null;
Microsoft.Office.Interop.Excel.Workbook xls = null;

Excel.Workbook workbook;
workbook = xlsApp.Workbooks.Open("Your excel file.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing);
foreach (Excel.Worksheet sheet in workbook.Sheets)
{
      if(sheet.Name.Equals("Which worksheet you want to map"))
      /// the table is converted to html table in clipboard
      string html = Clipboard.GetText(TextDataFormat.Html); // you actually can give him the HTML code of the table too
      // TextDataFormat.Html can be anyy html code
      File.WriteAllText(temporaryFilePath, html);
      Clipboard.SetText(html);
      sheet.Range(cellmapp).PasteSpecial();
      sheet.Columns.AutoFit();
      sheet.Rows.AutoFit();
}

现在,如果您从代码中编写一个简单的.exe可执行程序,您甚至可以安排它自动每天进行编写。 这是使用Microsoft.Office.Interop完成的,但它是一种中间媒体解决方案,但可能不是最好用的。