Beispiel #1
0
        private void ActivateSheet(NPOI.SS.UserModel.Sheet sheet)
        {
            DataTable dt    = new DataTable(sheet.SheetName);
            int       maxCx = 0;
            int       cy    = sheet.PhysicalNumberOfRows;

            for (int y = 0; y < cy; y++)
            {
                NPOI.SS.UserModel.Row row = sheet.GetRow(y);
                if (row != null)
                {
                    int cx = row.PhysicalNumberOfCells;
                    maxCx = Math.Max(maxCx, row.FirstCellNum + cx);
                }
            }
            int maxCy = sheet.FirstRowNum + cy;

            for (int x = 0; x < maxCx; x++)
            {
                DataColumn col = dt.Columns.Add("C" + (1 + x), typeof(String));
            }
            for (int vy = 0; vy < maxCy; vy++)
            {
                DataRow dr = dt.NewRow();
                if (vy >= sheet.FirstRowNum)
                {
                    int y = vy - sheet.FirstRowNum;
                    NPOI.SS.UserModel.Row row = sheet.GetRow(y);
                    for (int vx = 0; vx < maxCx; vx++)
                    {
                        dr[vx] = "";
                        if (row != null)
                        {
                            if (vx >= row.FirstCellNum)
                            {
                                int x = vx - row.FirstCellNum;
                                NPOI.SS.UserModel.Cell cell = row.GetCell(x);
                                dr[vx] = (cell != null) ? cell.ToString() : "";
                            }
                        }
                    }
                }
                dt.Rows.Add(dr);
            }

            gv.DataSource = dt;

            foreach (DataGridViewColumn col in gv.Columns)
            {
                col.ReadOnly = true;
            }

            gv.AutoResizeColumns();
            gv.AutoResizeRows();
        }