excel导出DataTable Aspose默认不读空白列。

感谢您的帮助和查看!
我在使用 Aspose.CellsAPI发现,Aspose.Cells默认不会读取空白的列,如下图:
列.jpg (298.7 KB)

有没有API设置可以读取空白列?

        Aspose.Cells.Workbook book = new Aspose.Cells.Workbook(excelPath);

        Aspose.Cells.Worksheet sheet = book.Worksheets[0];

        Aspose.Cells.Cells cells = sheet.Cells;

        Aspose.Cells.ExportTableOptions exportTableOptions = new Aspose.Cells.ExportTableOptions();
        //将单元格的字符串值导出到 DataTable.
        exportTableOptions.ExportAsString = true;
        //表示是否将第一行的数据导出到DataTable的列名。 默认值为false。
        exportTableOptions.ExportColumnName = true;
        //仅导出可见列
        exportTableOptions.PlotVisibleColumns = true;
        //仅导出可见行。
        exportTableOptions.PlotVisibleRows = true;

        DataTable dt_Import = cells.ExportDataTable(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, exportTableOptions);

@fhn123456,

感谢您的截图。

为什么在导出到 DataTable 时需要包括那些空白列? 能否请您分享您的模板 Excel 文件(请在附加前压缩),我们会检查出来。

我导出的时候不需要空白列,
我想知道api有没有可以设置导出空白列的开关。

@fhn123456,
目前没有设置导出空白列的开关。

@fhn123456,
根据你的截图,在你导出时设置totalColumns参数为cells.MaxDataColumn + 1是不会包括截图里的红框区域的。如果你想输出红框区域的空白列,请将totalColumns参数设置为CellsHelper.ColumnNameToIndex(“AP”) + 1。

请参考以下代码:

Aspose.Cells.Workbook book = new Aspose.Cells.Workbook(filePath + "sample.xlsx");

Aspose.Cells.Worksheet sheet = book.Worksheets[0];

Aspose.Cells.Cells cells = sheet.Cells;

Aspose.Cells.ExportTableOptions exportTableOptions = new Aspose.Cells.ExportTableOptions();
//将单元格的字符串值导出到 DataTable.
exportTableOptions.ExportAsString = true;
//表示是否将第一行的数据导出到DataTable的列名。 默认值为false。
exportTableOptions.ExportColumnName = true;
//仅导出可见列
exportTableOptions.PlotVisibleColumns = true;
//仅导出可见行。
exportTableOptions.PlotVisibleRows = true;   


DataTable dt_Import = cells.ExportDataTable(0, 0, cells.MaxDataRow + 1, CellsHelper.ColumnNameToIndex("AP") + 1, exportTableOptions);

如果你想过滤空白列,可以通过指定导出固定的列,也就是设置ExportTableOptions.Indexes来达到目的。
导出固定列请参考以下代码:

Aspose.Cells.Workbook book = new Aspose.Cells.Workbook(filePath + "sample.xlsx");

Aspose.Cells.Worksheet sheet = book.Worksheets[0];

Aspose.Cells.Cells cells = sheet.Cells;

Aspose.Cells.ExportTableOptions exportTableOptions = new Aspose.Cells.ExportTableOptions();
//将单元格的字符串值导出到 DataTable.
exportTableOptions.ExportAsString = true;
//表示是否将第一行的数据导出到DataTable的列名。 默认值为false。
exportTableOptions.ExportColumnName = true;
//仅导出可见列
exportTableOptions.PlotVisibleColumns = true;
//仅导出可见行。
exportTableOptions.PlotVisibleRows = true;


ArrayList indexList = new ArrayList();
int maxDataColumn = cells.MaxDataColumn;
for(int i = 0; i <= maxDataColumn; i++)
{
    // 只保留非空列
    if (!cells.IsBlankColumn(i))
    {
        indexList.Add(i);
    }
       
}
int indexCount = indexList.Count;
int[] indexes = new int[indexCount];
for (int i = 0; i < indexes.Length; i++)
{
    indexes[i] = (int)indexList[i];
}
exportTableOptions.Indexes = indexes;

DataTable dt_Import = cells.ExportDataTable(0, 0, cells.MaxDataRow + 1, CellsHelper.ColumnNameToIndex("AP") + 1, exportTableOptions);

非常感谢您的解答和帮助,谢谢您。

@fhn123456,

不客气。 很高兴知道建议的代码段可以满足您的需求。 如果您有任何疑问或意见,请随时与我们联系。