2017-03-09 435 views
0

我一直在使用Aspose.cell导入带有数据的Excel工作表。 Excel工作表由我为其分配十进制值的工资列组成。即使我从数据库中分配十进制值,列也被分配为字符串格式。 After Excel Import 一旦我双击每个单元格,它就转换为数字格式。 enter image description here 由于这个原因,我无法使用像“= SUM(M1:M20)”这样的公式。在使用Aspose.cells导入时无法使用Excel中的公式

我使用下面的函数使用的Aspose.Cells Excel的下载保护无效DownloadExcel(字符串psPlanNo,串psSuffix) { 尝试 { 的DataTable dtExcelData = GetDataTableValue(); dtExcelData.TableName = psPlanNo +“Template”;

 var workbook = new Workbook(); 
     var worksheet = workbook.Worksheets[0]; 
     worksheet.Cells.ImportDataTable(dtExcelData, true, "A1"); 
     worksheet.AutoFilter.Range = worksheet.Cells.FirstCell.Name + ":" + worksheet.Cells.LastCell.Name; 
     Response.Clear(); 
     Response.Buffer = true; 
     Response.Charset = ""; 
     Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
     Response.AddHeader("content-disposition", "attachment;filename=" + psPlanNo + psSuffix + ".xlsx"); 
     worksheet.AutoFitColumns(); 

     Aspose.Cells.Style style = worksheet.Cells["A1"].GetStyle(); 
     style.ForegroundThemeColor = new ThemeColor(ThemeColorType.Accent1, 0); 
     style.Font.Color = Color.White; 
     style.Pattern = BackgroundType.Solid; 

     for (int lnColumn = 0; lnColumn <= worksheet.Cells.MaxColumn; lnColumn++) 
      worksheet.Cells[0, lnColumn].SetStyle(style); 

     Cells cells = worksheet.Cells; 
     Aspose.Cells.Style fontStyle = new Aspose.Cells.Style(); 
     Aspose.Cells.Style stylefont = workbook.Styles[workbook.Styles.Add()]; 
     stylefont.Font.Name = "Calibri"; 
     stylefont.Font.Size = 12; 
     StyleFlag flag = new StyleFlag(); 
     flag.FontName = true; 
     flag.FontSize = true; 
     cells.ApplyStyle(stylefont, flag); 

     using (MemoryStream memoryStream = new MemoryStream()) 
     { 
      workbook.Save(memoryStream, SaveFormat.Xlsx); 
      memoryStream.WriteTo(Response.OutputStream); 
      HttpContext.Current.Response.Flush(); 
      HttpContext.Current.Response.SuppressContent = true; 
      HttpContext.Current.ApplicationInstance.CompleteRequest(); 
     } 
    } 
    catch (SqlException sql) 
    { 

     DbException(sql, MethodBase.GetCurrentMethod().Name); 
    } 
    catch (Exception ex) 
    { 
     GenericException(ex, MethodBase.GetCurrentMethod().Name); 
    } 
} 

有没有人有这个问题的解决方案?

由于提前

回答

1

我得到的解决方案,改变这一行代码

worksheet.Cells.ImportDataTable(dtExcelData, true, "A1"); 

到:

worksheet.Cells.ImportDataTable(dtExcelData, true, 0, 0, true, true); 

(注:最后一个布尔参数 “convertStringToNumber” 应设置为真)

现在它工作正常.. :)

+0

这是正确的解决方案。字符串值和整数值是不同的。因此,您必须在MS-Excel中按Enter键将字符串或文本值转换为数字或整数值。但是,当您将convertStringToNumber参数设置为true时,Aspose.Cells会自动将您的字符串值转换为数字值。 – shakeel