簡體   English   中英

在C#中使用NPOI將xlsx文件轉換為xls

[英]Convert xlsx file to xls using NPOI in c#

我有一個* .xlsx格式的Excel文件。 我想在我的MVC Web應用程序中將此文件轉換為* .xls格式。 在我的應用程序托管服務器中,沒有Microsoft.Office程序包。 請讓我知道我們如何使用C#在NPOI中實現它。

提前致謝。

我根據安迪的建議找到了解決方案。 這是為將XLSX轉換為XLS而定制的源代碼。

 public static class ConvertXLSXToXLS
{
    public static HSSFWorkbook ConvertWorkbookXSSFToHSSF(XSSFWorkbook source)
    {
        //Install-Package NPOI -Version 2.0.6
        HSSFWorkbook retVal = new HSSFWorkbook();
        for (int i = 0; i < source.NumberOfSheets; i++)
        {
            HSSFSheet hssfSheet = (HSSFSheet)retVal.CreateSheet(source.GetSheetAt(i).SheetName);

            XSSFSheet xssfsheet = (XSSFSheet)source.GetSheetAt(i);
            CopySheets(xssfsheet, hssfSheet, retVal);
        }
        return retVal;
    }

    private static void CopySheets(XSSFSheet source, HSSFSheet destination, HSSFWorkbook retVal)
    {
        int maxColumnNum = 0;
        Dictionary<int, XSSFCellStyle> styleMap = new Dictionary<int, XSSFCellStyle>();
        for (int i = source.FirstRowNum; i <= source.LastRowNum; i++)
        {
            XSSFRow srcRow = (XSSFRow)source.GetRow(i);
            HSSFRow destRow = (HSSFRow)destination.CreateRow(i);
            if (srcRow != null)
            {
                CopyRow(source, destination, srcRow, destRow, styleMap, retVal);
                if (srcRow.LastCellNum > maxColumnNum)
                {
                    maxColumnNum = srcRow.LastCellNum;
                }
            }
        }
        for (int i = 0; i <= maxColumnNum; i++)
        {
            destination.SetColumnWidth(i, source.GetColumnWidth(i));
        }
    }

    private static void CopyRow(XSSFSheet srcSheet, HSSFSheet destSheet, XSSFRow srcRow, HSSFRow destRow,
            Dictionary<int, XSSFCellStyle> styleMap, HSSFWorkbook retVal)
    {
        // manage a list of merged zone in order to not insert two times a
        // merged zone
        List<CellRangeAddress> mergedRegions = new List<CellRangeAddress>();
        destRow.Height = srcRow.Height;
        // pour chaque row
        for (int j = srcRow.FirstCellNum; j <= srcRow.LastCellNum; j++)
        {
            XSSFCell oldCell = (XSSFCell)srcRow.GetCell(j); // ancienne cell
            HSSFCell newCell = (HSSFCell)destRow.GetCell(j); // new cell
            if (oldCell != null)
            {
                if (newCell == null)
                {
                    newCell = (HSSFCell)destRow.CreateCell(j);
                }
                // copy chaque cell
                CopyCell(oldCell, newCell, styleMap, retVal);
                // copy les informations de fusion entre les cellules
                CellRangeAddress mergedRegion = GetMergedRegion(srcSheet, srcRow.RowNum,
                        (short)oldCell.ColumnIndex);

                if (mergedRegion != null)
                {
                    CellRangeAddress newMergedRegion = new CellRangeAddress(mergedRegion.FirstRow,
                            mergedRegion.LastRow, mergedRegion.FirstColumn, mergedRegion.LastColumn);
                    if (IsNewMergedRegion(newMergedRegion, mergedRegions))
                    {
                        mergedRegions.Add(newMergedRegion);
                        destSheet.AddMergedRegion(newMergedRegion);
                    }

                    if (newMergedRegion.FirstColumn == 0 && newMergedRegion.LastColumn == 6 && newMergedRegion.FirstRow == newMergedRegion.LastRow)
                    {
                        HSSFCellStyle style2 = (HSSFCellStyle)retVal.CreateCellStyle();
                        style2.VerticalAlignment = VerticalAlignment.Center;
                        style2.Alignment = HorizontalAlignment.Left;
                        style2.FillForegroundColor = HSSFColor.Teal.Index;
                        style2.FillPattern = FillPattern.SolidForeground;

                        for (int i = destRow.FirstCellNum; i <= destRow.LastCellNum; i++)
                        {
                            if (destRow.GetCell(i) != null)
                                destRow.GetCell(i).CellStyle = style2;
                        }
                    }
                }
            }
        }



    }

    private static void CopyCell(XSSFCell oldCell, HSSFCell newCell, Dictionary<int, XSSFCellStyle> styleMap, HSSFWorkbook retVal)
    {
        if (styleMap != null)
        {
            int stHashCode = oldCell.CellStyle.Index;
            XSSFCellStyle sourceCellStyle = null;
            if (styleMap.TryGetValue(stHashCode, out sourceCellStyle)) { }

            HSSFCellStyle destnCellStyle = (HSSFCellStyle)newCell.CellStyle;
            if (sourceCellStyle == null)
            {
                sourceCellStyle = (XSSFCellStyle)oldCell.Sheet.Workbook.CreateCellStyle();
            }
            // destnCellStyle.CloneStyleFrom(oldCell.CellStyle);
            if (!styleMap.Any(p => p.Key == stHashCode))
            {
                styleMap.Add(stHashCode, sourceCellStyle);
            }

            destnCellStyle.VerticalAlignment = VerticalAlignment.Top;
            newCell.CellStyle = (HSSFCellStyle)destnCellStyle;
        }
        switch (oldCell.CellType)
        {
            case CellType.String:
                newCell.SetCellValue(oldCell.StringCellValue);
                break;
            case CellType.Numeric:
                newCell.SetCellValue(oldCell.NumericCellValue);
                break;
            case CellType.Blank:
                newCell.SetCellType(CellType.Blank);
                break;
            case CellType.Boolean:
                newCell.SetCellValue(oldCell.BooleanCellValue);
                break;
            case CellType.Error:
                newCell.SetCellErrorValue(oldCell.ErrorCellValue);
                break;
            case CellType.Formula:
                newCell.SetCellFormula(oldCell.CellFormula);
                break;
            default:
                break;
        }

    }


    private static CellRangeAddress GetMergedRegion(XSSFSheet sheet, int rowNum, short cellNum)
    {
        for (int i = 0; i < sheet.NumMergedRegions; i++)
        {
            CellRangeAddress merged = sheet.GetMergedRegion(i);
            if (merged.IsInRange(rowNum, cellNum))
            {
                return merged;
            }
        }
        return null;
    }

    private static bool IsNewMergedRegion(CellRangeAddress newMergedRegion,
            List<CellRangeAddress> mergedRegions)
    {
        return !mergedRegions.Contains(newMergedRegion);
    }
}

這是我的建議:

總的來說,這是可能的,但並不容易。

XLS文件格式由HSSFWorkbook類(並根據HSSFSheet等)處理。 XLSX文件格式由XSSFWorkbook類(和XSSFSheet等)處理。

因此,為了將文件從XLSX轉換為XLS您需要:

  • 創建XSSFWorkbook ,從文件中加載數據;
  • 創建新的HSSFWorkbook ;
  • 為原始文件中的每個工作表創建HSSFSheet並將所有數據從原始工作表復制到該新表中
  • 將您的HSSFWorkbook寫入文件。

因此,這里要做很多工作,這是非常復雜的任務。

在您的情況下,NPOI可能不是最佳解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM