前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test1.aspx.cs" Inherits="WebApplication1.Excel1.tohtml.test1" %>
<!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 runat="server">
<title>EXCEL转换成html文件</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Button ID="Button1" runat="server" Text="开始转换" onclick="Button1_Click" />
</form>
</body>
</html>
后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using Microsoft.Office.Interop.Excel;
using System.Diagnostics;
using WebApplication1.common;
namespace WebApplication1.Excel1.tohtml
{
public partial class test1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
excel_to.ExcelConvertToHtml(Server.MapPath("../data/test1.xls"), Server.MapPath("../data/test2.html"));
}
}
}
类:
public static void ExcelConvertToHtml(string xlsPath, string htmlPath)
{
try
{
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
app.Visible = false;
Object o = Missing.Value;
/**/
/// _Workbook xls=app.Workbooks.Open(xlsPath,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o);
_Workbook xls = app.Workbooks.Open(xlsPath, o, o, o, o, o, o, o, o, o, o, o, o);
object fileName = htmlPath;
object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;//Html
// xls.SaveAs(ref fileName,ref format,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o);
if (System.IO.File.Exists(fileName.ToString()) == true)
{
System.IO.Directory.Delete(fileName.ToString(), true);
}
xls.SaveAs(fileName, format, o, o, o, o, XlSaveAsAccessMode.xlExclusive, o, o, o, o);
object t = true;
app.Quit();
Process[] myProcesses = Process.GetProcessesByName("EXCEL");
foreach (Process myProcess in myProcesses)
{
myProcess.Kill();
}
}
catch (Exception ex)
{
System.Console.Write(ex.Message);
}
}
