在C#中添加新的Excel工作表

时间:2013-02-05 07:19:50

标签: excel c#-4.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
    }

由于

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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);
        }