0

我正在使用 NPOI 插件将内容导出为 Excel 文件。问题是,当导出文件达到最小导出大小(比如,65535)时,它会抛出超出大小限制的异常。所以我决定去动态创建另一个工作表来修改65535之后的记录。但我不知道如何编写这个逻辑。请指导我如何做到这一点的逻辑。

代码

                GridModel model = GetItems().AsQueryable().ToGridModel(page, int.MaxValue, orderBy, string.Empty, filter);
        var orders = model.Data.Cast<ViewModel>();

        //Create new Excel workbook
        var workbook = new HSSFWorkbook();

        //Create new Excel sheet
        var sheet = workbook.CreateSheet();
        //int index = workbook.AddPicture(@"D:\Temp\2\autocomplete tables", HSS);
        //    HSSFPicture signaturePicture = patriarch.CreatePicture(anchor, index);
        ////(Optional) set the width of the columns
        sheet.SetColumnWidth(0, 10 * 256);
        sheet.SetColumnWidth(1, 20 * 256);
        sheet.SetColumnWidth(2, 110 * 256);
        sheet.SetColumnWidth(3, 30 * 256);

        //Create a header row
        var headerRow = sheet.CreateRow(0);
        //Set the column names in the header row
        headerRow.CreateCell(0).SetCellValue("Itemid");
        headerRow.CreateCell(1).SetCellValue("ItemNo");
        headerRow.CreateCell(2).SetCellValue("Title");
        headerRow.CreateCell(3).SetCellValue("Status");

        //(Optional) freeze the header row so it is not scrolled
        sheet.CreateFreezePane(0, 1, 0, 1);

        int rowNumber = 1;

        //Populate the sheet with values from the grid data
        foreach (ViewModel order in orders)
        {
            //Create a new row
            if (rowNumber < 65535)
            {
                var row = sheet.CreateRow(rowNumber++);
                //Set values for the cells
                row.CreateCell(0).SetCellValue(order.Itemid);
                row.CreateCell(1).SetCellValue(order.INo);
                row.CreateCell(2).SetCellValue(order.BTags);
                row.CreateCell(3).SetCellValue(order.Sid);
            }
            else
            {

            }
        }

        //Write the workbook to a memory stream
        MemoryStream output = new MemoryStream();
        workbook.Write(output);

        //Return the result to the end user

        return File(output.ToArray(),   //The binary data of the XLS file
            "application/vnd.ms-excel", //MIME type of Excel files
            "Items.xls");     //Suggested file name in the "Save as" dialog which will be displayed to the end user
    }

谢谢

4

2 回答 2

1
       object[,] objectrange;
        Excel.Range ExcelRangeNotFoundArticle;
        Excel.Application ActiveExelAplication = Globals.ThisAddIn.Application as Excel.Application;

        Excel.Worksheet ExNewWorksheet = ActiveExelAplication.Sheets.Add() as Excel.Worksheet;/*add new sheet here*/

        ExNewWorksheet.Name = ("sheetName");
        Excel.Worksheet ExWorksheet = ActiveExelAplication.ActiveSheet as Excel.Worksheet;
        ExcelRangeNotFoundArticle = ExWorksheet.get_Range("a1", "am12") as Excel.Range;
        objectrange= (object[,])ExcelRangeNotFoundArticle.Value;
于 2013-10-29T15:59:08.140 回答
0

希望以下内容对您有用:

    foreach (ViewModel order in orders)
    {

    if (rowNumber % 65535 == 0 )
        {   
    sheet = workbook.CreateSheet();
        sheet.SetColumnWidth(0, 10 * 256);
        sheet.SetColumnWidth(1, 20 * 256);
        sheet.SetColumnWidth(2, 110 * 256);
        sheet.SetColumnWidth(3, 30 * 256);

        headerRow.CreateCell(0).SetCellValue("Itemid");
        headerRow.CreateCell(1).SetCellValue("ItemNo");
        headerRow.CreateCell(2).SetCellValue("Title");
        headerRow.CreateCell(3).SetCellValue("Status");

    sheet.CreateFreezePane(0, 1, 0, 1);
    //If you want row number should start from 1. Otherwise comment it
    rowNumber = 1;
    }   
        //Create a new row
        if (rowNumber < 65535)
        {
            var row = sheet.CreateRow(rowNumber++);
            //Set values for the cells
            row.CreateCell(0).SetCellValue(order.Itemid);
            row.CreateCell(1).SetCellValue(order.INo);
            row.CreateCell(2).SetCellValue(order.BTags);
            row.CreateCell(3).SetCellValue(order.Sid);
        }
于 2013-02-05T07:30:02.720 回答