c# excel转html
时间: 2023-10-09 20:11:34 浏览: 32
你可以使用 C# 中的 Microsoft.Office.Interop.Excel 库来读取 Excel 文件,并将其转换为 HTML 格式。以下是一个示例代码:
```csharp
using System;
using System.IO;
using System.Text;
using Microsoft.Office.Interop.Excel;
namespace ExcelToHtml
{
class Program
{
static void Main(string[] args)
{
var excelFilePath = "example.xlsx";
var htmlFilePath = "example.html";
var app = new Application();
var workbook = app.Workbooks.Open(excelFilePath);
var worksheet = workbook.Worksheets[1];
var range = worksheet.UsedRange;
var rowCount = range.Rows.Count;
var columnCount = range.Columns.Count;
var sb = new StringBuilder();
sb.Append("<table>");
for (int i = 1; i <= rowCount; i++)
{
sb.Append("<tr>");
for (int j = 1; j <= columnCount; j++)
{
var cellValue = range.Cells[i, j].Value;
sb.Append("<td>");
sb.Append(cellValue);
sb.Append("</td>");
}
sb.Append("</tr>");
}
sb.Append("</table>");
workbook.Close();
app.Quit();
File.WriteAllText(htmlFilePath, sb.ToString());
Console.WriteLine("Excel converted to HTML successfully!");
}
}
}
```
在该示例中,我们使用 Microsoft.Office.Interop.Excel 库读取 example.xlsx 文件,并将其转换为 HTML 格式。将该代码保存为 C# 项目并运行,即可生成 example.html 文件。
阅读全文
相关推荐














