简体   繁体   English

如何适合Excel工作表中的列

[英]How to fit the columns in an excel sheet

I'm writing a list to an excel sheet generated using EPPlus with .xlsx extension. 我正在将列表EPPlus使用扩展名为.xlsx EPPlus生成的excel工作表中。 Then using worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns(); 然后使用worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns(); method I tried to fit the columns. 我试图适合列的方法。

This is how I write data 这就是我写数据的方式

using (ExcelPackage xlPackage = new ExcelPackage(newFile))
      {
        System.Data.DataTable dt = new System.Data.DataTable();
        var ws = xlPackage.Workbook.Worksheets.FirstOrDefault(x => x.Name == language.Culture);
        if (ws == null)
         {
          int i = 1, j = 0;
           worksheet = xlPackage.Workbook.Worksheets.Add(language.Culture);
           foreach (ExcelFields fieldValues in UnmatchedFieldList)
           {
                 //code
            }
           else
             {
                int i = 0;
                worksheet = xlPackage.Workbook.Worksheets[language.Culture];
                colCount = worksheet.Dimension.End.Column;
                 rowCount = worksheet.Dimension.End.Row;
                 foreach (ExcelFields fieldValues in UnmatchedFieldList)
                 {
                       worksheet.Cells[rowCount + 1, count + 1].Value = itemName;
                  }
                 worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();
                 xlPackage.Save();
             }

I read data as 我读数据为

            string sheetName = language.Culture;
            var excelFile = new ExcelQueryFactory(excelPath);
            IQueryable<Row> excelSheetValues = from workingSheet in excelFile.Worksheet(sheetName) select workingSheet;
            string[] headerRow = excelFile.GetColumnNames(sheetName).ToArray();

At headerRow it is throwing the below error 在headerRow它抛出以下错误

When I'm trying to read the data from excel it is throwing an exception 当我尝试从excel读取数据时,它抛出异常

External table is not in the expected format 外部表格的格式不正确

I found out,this is due to the columns are not formatted(width) correctly. 我发现,这是由于列未正确格式化(宽度)。 When I manually set the columns width by double clicking the cell and run the code it is working fine 当我通过双击单元格并设置代码来手动设置列宽时,它工作正常

So I want to achieve this using code 所以我想用代码来实现

External table is not in the expected format exception is occurred because of exception of connection string so there fore check your connection string with following sample 外部表未以预期的格式发生异常,因为连接字符串异常,所以请使用以下示例检查连接字符串

public static string docPath= @"C:\sourcefolder\myfile.xlsx";
public static string ConnectionString= "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + docPath + ";Extended Properties=Excel 12.0;";

or if you use LinqToExcel then check whether you have set setting the DatabaseEngine property like as follows 或者如果您使用LinqToExcel,则检查是否已设置如下所示设置DatabaseEngine属性

public string docPath= @"C:\sourcefolder\myfile.xlsx";
var excelFile = new ExcelQueryFactory(docPath);
excelFile.DatabaseEngine = DatabaseEngine.Ace;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM