如何将图形添加到通过TMS Flexcel生成的Excel文件?

时间:2015-07-01 10:19:28

标签: c# excel tms flexcel

我有一个xlsx文件,用作TMS Flexcel的模板。我用数据填充它,它在漂亮的表中呈现它。到目前为止一切都很好。 问题是当我想添加图表时。

生成的xlsx文件似乎已损坏,Excel在我打开它时会尝试恢复它。不幸的是它失败了,所以生成的文件没有图形。

有没有人遇到过它?如何解决它?

我收到的错误信息是

 Excel completed file level validation and repair. 
 Some parts of this workbook may have been repaired or discarded.
 Removed Part: Drawing shape.

指向Flexcel网站的链接:http://www.tmssoftware.com/site/default.asp

2 个答案:

答案 0 :(得分:1)

我们取得的一些成功是: 1.将一些虚拟数据添加到将存储实际数据的表中。 1.根据虚拟数据创建图形并按照您希望的方式对其进行格式化。

当数据导出到Excel时,它将替换虚拟数据并显示在图表中。

TMS对xlsx文件的支持也非常有限。请尝试使用xls文件。

答案 1 :(得分:0)

David Voltrubec。我们遇到了类似的问题,我们想在同一张纸上绘制2张表的图形。 Flexcel不动态支持图形绘制,因为它没有设置图形范围的属性。我做的是 1.将表写入XLSX文件并保存。 2.使用第三方库(MIT许可证)打开它,该库使用Open XML SDK并指定要绘制的图形的范围和类型。

这样我也可以在动态表格上生成图表。

下面是我使用SpreadSheetLight库的代码提取。 List<DrawExcelChart.DynamicExcelChart>是保存startRange的通用列表,GetColumnAlphabetAndNumber是将列/行转换为Excel字母数字单元格引用。

private void DrawChartInExcelUsingSpreadSheetLight(string astrFileName, string astrFilePath, out string astrAlteredFileName)
{
    List<DrawExcelChart.DynamicExcelChart> ActualCellStartEndRanges;
    string finalFilePath = string.Empty;
    int WorkSheetsCount = 0;
    string newFilePath = string.Empty;
    double ChartYpositionStart = 0;
    double ChartYpositionEnd = 0;
    string endPointAlphabet = string.Empty;
    string endPointNumber = string.Empty;
    string startPointAlphabet = string.Empty;
    string startPointNumber = string.Empty;

    DrawExcelChart myExcelChartObject = new DrawExcelChart();
    SLChart SSLChartObject = null;
    SLDocument SSLDocumentObject = null;
    try
    {
        DecideCellStartEndRanges(out ActualCellStartEndRanges);
        finalFilePath = astrFilePath + "\\" + astrFileName;
        newFilePath = astrFilePath + "\\" + astrFileName.Replace("haschart", "");

        using (SSLDocumentObject = new SLDocument(finalFilePath))
        {
            WorkSheetsCount = SSLDocumentObject.GetSheetNames().Count;
            for (int workSheetCount = 0; workSheetCount < WorkSheetsCount; workSheetCount++)
            {
                SSLDocumentObject.SelectWorksheet(SSLDocumentObject.GetSheetNames()[workSheetCount]);

                myExcelChartObject.GetColumnAlphabetAndNumber(ActualCellStartEndRanges[workSheetCount].FirstChart.startRange, out startPointAlphabet, out startPointNumber);
                myExcelChartObject.GetColumnAlphabetAndNumber(ActualCellStartEndRanges[workSheetCount].FirstChart.endRange, out endPointAlphabet, out endPointNumber);

                ChartYpositionStart = Convert.ToInt32(endPointNumber) + 1;
                ChartYpositionEnd = Convert.ToInt32(endPointNumber) + 2;

                SSLDocumentObject.SetRowHeight(Convert.ToInt32(startPointNumber), Convert.ToInt32(endPointNumber) + 1, 15);

                SSLChartObject = SSLDocumentObject.CreateChart(ActualCellStartEndRanges[workSheetCount].FirstChart.startRange, ActualCellStartEndRanges[workSheetCount].FirstChart.endRange);
                SSLChartObject.SetChartType(SpreadsheetLight.Charts.SLLineChartType.LineWithMarkers);
                SSLChartObject.SetChartPosition(ChartYpositionStart, 1, ChartYpositionEnd, 14);
                SSLDocumentObject.SetRowHeight(Convert.ToInt32(ChartYpositionEnd), 180);
                SSLChartObject.SetChartStyle(SpreadsheetLight.Charts.SLChartStyle.Style2);
                SSLChartObject.Border.SetAutomaticColor();
                SSLChartObject.Legend.LegendPosition = DocumentFormat.OpenXml.Drawing.Charts.LegendPositionValues.Right;
                SSLDocumentObject.InsertChart(SSLChartObject);
            }
            SSLDocumentObject.SelectWorksheet(SSLDocumentObject.GetSheetNames()[0]);
            SSLDocumentObject.SaveAs(newFilePath);
        }

        astrAlteredFileName = astrFileName.Replace("haschart", "");
    }
    catch (Exception obj_Except)
    {
        astrAlteredFileName = string.Empty;
    }
    finally
    {
        ActualCellStartEndRanges = null;
        finalFilePath = string.Empty;
        WorkSheetsCount = 0;
        newFilePath = string.Empty;
        ChartYpositionStart = 0;
        ChartYpositionEnd = 0;
        endPointAlphabet = string.Empty;
        endPointNumber = string.Empty;
        startPointAlphabet = string.Empty;
        startPointNumber = string.Empty;
        myExcelChartObject = null;
        SSLChartObject = null;
        SSLDocumentObject = null;
    }
}