找回密码
 立即注册

QQ登录

只需一步,快速开始

lkxtracy

金牌服务用户

12

主题

57

帖子

195

积分

金牌服务用户

积分
195

[已处理] Excel导入问题

lkxtracy
金牌服务用户   /  发表于:2018-9-10 16:56  /   查看:3273  /  回复:8
导入Excel后,遍历单元格设置富文本格式单元格,性能太差,处理一页,差不多需要一分钟到几分钟不等
任意Excel文件即可重现


参考代码:
fpSpread.OpenExcel("xxx");
for(int sheetindex  0;sheetindex < fpSpread.Sheets.Count;++sheetindex)
{
SheetView sheet = fpSpread.Sheets[sheetindex];
sheet.ColumnCount = sheet.GetLastNonEmptyColumn(NonEmptyItemFlag.Data) + 1;
sheet.RowCount = sheet.GetLastNonEmptyRow(NonEmptyItemFlag.Data) + 1;

for (int row = 0; row < sheet.RowCount; ++row)
            {
                for (int col = 0; col < sheet.ColumnCount; ++col)
                {
                    if (!(sheet.Cells[row, col].CellType is RichTextCellType))
                    {
                        sheet.Cells[row, col].CellType = new RichTextCellType();
                    }
                }
            }

}

8 个回复

倒序浏览
lkxtracy
金牌服务用户   /  发表于:2018-9-10 16:58:21
沙发
另外:通过Spread新建出来的表格,不存在这种问题
回复 使用道具 举报
dexteryao讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2018-9-10 17:54:30
板凳
如果您是要给所有单元格设置richcell ,建议用DefaultStyle

            fpSpread1.ActiveSheet.DefaultStyle.CellType = new RichTextCellType();
或者可以用cells,直接全部设置。
            fpSpread1.ActiveSheet.Cells[0, 0, fpSpread1.ActiveSheet.RowCount - 1, fpSpread1.ActiveSheet.ColumnCount - 1].CellType = new RichTextCellType();

您大概有多少行列?
回复 使用道具 举报
品茗-3
金牌服务用户   /  发表于:2018-9-10 19:37:47
地板
dexteryao 发表于 2018-9-10 17:54
如果您是要给所有单元格设置richcell ,建议用DefaultStyle

            fpSpread1.ActiveSheet.Default ...

导入的肯定是对文本类型的单独设置富文本啊,又不是新建,总不能把日期类型的都设成富文本吧
回复 使用道具 举报
dexteryao讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2018-9-11 09:02:15
5#
fpSpread1.ActiveSheet.DefaultStyle.CellType = new RichTextCellType();
这种方案就是如果单元格有CellType,就用自己的,没有就默认Rich。
如果上述方案还不能满足您的要求,请提供下您的测试excel模板,或者告知您这个excel有多少个sheet多少行多少列。
如下截图是我测试的结果,导入excel后设置所有单元格,时间不超过1s

image.png925345153.png
回复 使用道具 举报
lkxtracy
金牌服务用户   /  发表于:2018-9-11 09:53:38
6#
dexteryao 发表于 2018-9-11 09:02
fpSpread1.ActiveSheet.DefaultStyle.CellType = new RichTextCellType();
这种方案就是如果单元格有Cell ...

fpSpread1.ActiveSheet.DefaultStyle.CellType = new RichTextCellType();
或者可以用cells,直接全部设置。
            fpSpread1.ActiveSheet.Cells[0, 0, fpSpread1.ActiveSheet.RowCount - 1, fpSpread1.ActiveSheet.ColumnCount - 1].CellType = new RichTextCellType();

这两种写法有什么区别?
回复 使用道具 举报
lkxtracy
金牌服务用户   /  发表于:2018-9-11 10:33:57
8#
private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog() { Filter = @" Excel文件|*.xlsx;*.xls" };
            if (dialog.ShowDialog(this) == DialogResult.OK)
            {
                fpSpread1.OpenExcel(dialog.FileName,ExcelOpenFlags.NoFlagsSet);
                foreach (SheetView sheet in fpSpread1.Sheets)
                {
                    sheet.ColumnCount = sheet.GetLastNonEmptyColumn(NonEmptyItemFlag.Data) + 1;
                    sheet.RowCount = sheet.GetLastNonEmptyRow(NonEmptyItemFlag.Data) + 1;
                    //for (int row = 0; row < sheet.RowCount; ++row)
                    //{
                    //    for (int col = 0; col < sheet.ColumnCount; ++col)
                    //    {
                    //        sheet.Cells[row, col].CellType = null;
                    //        sheet.Cells[row, col].CellType = new RichTextCellType();
                    //    }
                    //}
                    sheet.Cells[0, 0, sheet.RowCount - 1, sheet.ColumnCount - 1].CellType = new RichTextCellType();
                }
            }
        }
应该是这个异常导致的


导入Excel异常.png

测试Excel导入.xls

11.5 KB, 下载次数: 76

回复 使用道具 举报
lkxtracy
金牌服务用户   /  发表于:2018-9-11 14:08:48
9#
dexteryao 发表于 2018-9-11 09:02
fpSpread1.ActiveSheet.DefaultStyle.CellType = new RichTextCellType();
这种方案就是如果单元格有Cell ...

那我发的那个Excel文件,重现出来了没?fpSpread1.ActiveSheet.DefaultStyle.CellType = new RichTextCellType();没效果,fpSpread1.ActiveSheet.Cells[0, 0, fpSpread1.ActiveSheet.RowCount - 1, fpSpread1.ActiveSheet.ColumnCount - 1].CellType = new RichTextCellType();一样卡。
回复 使用道具 举报
dexteryao讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2018-9-12 12:09:32
10#
fpSpread1.ActiveSheet.DefaultStyle.CellType = new RichTextCellType(); 这个是给sheet设置一个默认样式,实际单元格没样式,是全局的,所以不用遍历所有单元格。

测试您提供的excel没有发现慢和异常
  1.       fpSpread1.OpenExcel(@"C:\Users\Dexter\Desktop\测试Excel导入.xls");


  2.             label1.Text = DateTime.Now.ToString();
  3.             foreach (SheetView sheet in fpSpread1.Sheets)
  4.             {
  5.                 sheet.ColumnCount = sheet.GetLastNonEmptyColumn(NonEmptyItemFlag.Data) + 1;
  6.                 sheet.RowCount = sheet.GetLastNonEmptyRow(NonEmptyItemFlag.Data) + 1;
  7.                 //for (int row = 0; row < sheet.RowCount; ++row)
  8.                 //{
  9.                 //    for (int col = 0; col < sheet.ColumnCount; ++col)
  10.                 //    {
  11.                 //        sheet.Cells[row, col].CellType = null;
  12.                 //        sheet.Cells[row, col].CellType = new RichTextCellType();
  13.                 //    }
  14.                 //}
  15.                 sheet.Cells[0, 0, sheet.RowCount - 1, sheet.ColumnCount - 1].CellType = new RichTextCellType();
  16.             }
  17.             label2.Text = DateTime.Now.ToString();
复制代码

image.png515121154.png
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部