简体   繁体   English

使用Open Xml SDK和LINQ在Excel中插入文本

[英]Inserting text in Excel using Open Xml SDK and LINQ

I have been trying really hard but my excel sheet is not populating as per my expectations. 我一直在努力尝试,但是我的excel工作表并没有按照我的期望进行填充。 If the content is having string datatype then sheet is showing '0' in place of that, howsoever hard I tried by using conversions. 如果内容具有字符串数据类型,则工作表将显示'0'代替它,无论如何我都尝试使用转换。 I am pasting my code below if any one can help me: 如果有人可以帮助我,请在下面粘贴我的代码:

  public static void WriteExcelDocument(string FilePath)
    {
        try
        {
            using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(FilePath, true))
            {
                WorkbookPart workbookPart = spreadSheet.WorkbookPart;
                IEnumerable<Sheet> Sheets = spreadSheet.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>().Where(s => s.Name == "data");
                if (Sheets.Count() == 0)
                {
                    // The specified worksheet does not exist. 
                    return;
                }
                string relationshipId = Sheets.First().Id.Value;
                WorksheetPart worksheetPart = (WorksheetPart)spreadSheet.WorkbookPart.GetPartById(relationshipId);
                SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
                int index = 2;
                SpectrumNewEntities context = new SpectrumNewEntities();
                var q = from result in context.Appraisers
                        select result;
                foreach (var g in q)
                {
                    string Name = g.AppraiserName!=null?g.AppraiserName:String.Empty;
                    string city = g.City != null ? g.City : String.Empty;
                    string Address = g.Address != null ? g.Address : "NA";
                    int AppId = g.AppraiserAppraiserCompanyId != null ? (int)g.AppraiserAppraiserCompanyId : 0;
                    string email = g.Email != null ? g.Email : String.Empty;
                    Row contentRow = CreateContentRow(index, Name, city, Address, AppId,email);
                    index++;
                    sheetData.AppendChild(contentRow);
                }
                // Save the worksheet.
                worksheetPart.Worksheet.Save();
            }
        }
        catch (Exception)
        {

            throw;
        }
    }

    private static Row CreateContentRow(int index, string Name, string city, string Address, int AppId, string email)
    {
        try
        {
            //Create new row
            Row r = new Row();
            r.RowIndex = (UInt32)index;

            //First cell is a text cell, so create it and append it
            Cell firstCell = CreateTextCell(headerColumns[0], Name, index);
            r.AppendChild(firstCell);//

            //create cells that contain data
            for (int i = 1; i < headerColumns.Length; i++)
            {
                Cell c = new Cell();
                c.CellReference = headerColumns[i] + index;
                CellValue v = new CellValue();
                if (i == 1)
                {
                    v.Text = city.ToString();
                }
                if (i == 2)
                {
                    v.Text = Address.ToString();
                }
                if (i == 3)
                {
                    v.Text =AppId.ToString();
                }
                if (i == 4)
                {
                    v.Text = email.ToString();
                }
                c.AppendChild(v);
                r.AppendChild(c);

            }
            return r;
        }
        catch (Exception)
        {

            throw;
        }
    }
    private static Cell CreateTextCell(string header, string Name,int index)
    {
        try
        {
            //Create a new inline string cell
            Cell c = new Cell();
            c.DataType = CellValues.InlineString;
            c.CellReference = header + index;

            //Add text to text cell
            InlineString inlineString = new InlineString();
            Text t = new Text();
            t.Text = Name;
            inlineString.AppendChild(t);
            c.AppendChild(inlineString);
            return c;
        }
        catch (Exception)
        {

            throw;
        }
    }

I am not getting why display is something like this? 我不明白为什么显示是这样的? 在此处输入图片说明

Use the CreateTextCell method to add all the text cells to the row; 使用CreateTextCell方法将所有文本单元格添加到该行; like you are doing for the Name field. 就像您在“ Name字段中所做的一样。

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

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