WinForm 使用Html生成的.xlsx文件打不开

winform 使用html导出RadChartView中数据到Excel文件,xls格式没问题,xlsx文件打不开。
提示如下:Excel 无法打开文件“*.xlsx”,因为文件格式或文件扩展名无效。请确定文件未损坏,并且文件扩展名与文件的格式匹配。
代码如下:
try
{
StringBuilder htmlStr = new StringBuilder();
htmlStr.Append("");
htmlStr.Append("

");
htmlStr.Append("");
htmlStr.Append("");
htmlStr.Append("");
htmlStr.Append("");
htmlStr.Append("");
if (this.radChartView1.Series.Count > 0)
{
foreach (var lineSeries in this.radChartView1.Series)
{
htmlStr.Append("");
}
}
htmlStr.Append("");
htmlStr.Append("
");
htmlStr.Append("");
htmlStr.Append($"");
foreach (var dataPoint in lineSeries.DataPoints)
{
CategoricalDataPoint point = dataPoint as CategoricalDataPoint;
if (point != null)
{
htmlStr.Append("");
htmlStr.Append($"");
htmlStr.Append($"");
htmlStr.Append("");
}
}
htmlStr.Append("
{lineSeries.Name}
{point.Category}{point.Label}
");
htmlStr.Append("
");
htmlStr.Append("");
htmlStr.Append("");
            using (SaveFileDialog saveFileDialog = new SaveFileDialog())
            {
                saveFileDialog.Filter = $"xls files(*.xls)|*.xls|xlsx files(*.xlsx)|*.xlsx";
                saveFileDialog.RestoreDirectory = true;
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    StreamWriter writer = new StreamWriter(saveFileDialog.FileName, false);
                    writer.WriteLine(htmlStr.ToString());
                    writer.Close();
                }
            }
        }
        catch (Exception exception)
        {
            RadMessageBox.Show(exception.Message);
        }

代码贴出来好像有点问题,重新贴代码:
try
{
StringBuilder htmlStr = new StringBuilder();
htmlStr.Append("");
htmlStr.Append("

");
htmlStr.Append("");
htmlStr.Append("");
htmlStr.Append("");
htmlStr.Append("");
htmlStr.Append("");
if (this.radChartView1.Series.Count > 0)
{
foreach (var lineSeries in this.radChartView1.Series)
{
htmlStr.Append("");
}
}
htmlStr.Append("");
htmlStr.Append("
");
htmlStr.Append("");
htmlStr.Append($"");
foreach (var dataPoint in lineSeries.DataPoints)
{
CategoricalDataPoint point = dataPoint as CategoricalDataPoint;
if (point != null)
{
htmlStr.Append("");
htmlStr.Append($"");
htmlStr.Append($"");
htmlStr.Append("");
}
}
htmlStr.Append("
{lineSeries.Name}
{point.Category}{point.Label}
");
htmlStr.Append("
");
htmlStr.Append("");
htmlStr.Append("");
            using (SaveFileDialog saveFileDialog = new SaveFileDialog())
            {
                saveFileDialog.Filter = $"xls files(*.xls)|*.xls|xlsx files(*.xlsx)|*.xlsx";
                saveFileDialog.RestoreDirectory = true;
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    StreamWriter writer = new StreamWriter(saveFileDialog.FileName, false);
                    writer.WriteLine(htmlStr.ToString());
                    writer.Close();
                }
            }
        }
        catch (Exception exception)
        {
            RadMessageBox.Show(exception.Message);
        }

那就不用excel驱动打开,直接用txt文本方式打开。

打不开很正常,因为你生成的根本不是excel 2007的格式,只是纯文本文件,而掩耳盗铃地使用了xlsx后缀而已。要生成真正的xlsx文件,可以使用npoi这个库。