相关疑难解决方法(0)

打开xml excel读取单元格值

我正在使用Open XML SDK打开Excel xlsx文件,并尝试读取每个工作表中位置A1的单元格值.我使用以下代码:

using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(openFileDialog1.FileName, false))
{
    var sheets = spreadsheetDocument.WorkbookPart.Workbook.Descendants<Sheet>();

    foreach (Sheet sheet in sheets)
    {
        WorksheetPart worksheetPart = (WorksheetPart)spreadsheetDocument.WorkbookPart.GetPartById(sheet.Id);
        Worksheet worksheet = worksheetPart.Worksheet;

        Cell cell = GetCell(worksheet, "A", 1);

        Console.Writeline(cell.CellValue.Text);
     }
}

private static Cell GetCell(Worksheet worksheet, string columnName, uint rowIndex)
{
     Row row = GetRow(worksheet, rowIndex);

     if (row == null)
         return null;

     return row.Elements<Cell>().Where(c => string.Compare
               (c.CellReference.Value, columnName +
               rowIndex, true) == 0).First();
}

// Given a worksheet and a row index, return …
Run Code Online (Sandbox Code Playgroud)

c# openxml openxml-sdk

47
推荐指数
3
解决办法
7万
查看次数

使用OpenXml sdk 2.0创建Excel文档

我使用OpenXml SDK 2.0创建了一个Excel文档,现在我必须设置它的样式,但我不能.

我不知道如何绘制背景颜色或更改不同单元格中的字体大小.

我创建单元格的代码是:

private static Cell CreateTextCell(string header, string text, UInt32Value index)
{
    Cell c = new Cell();
    c.DataType = CellValues.InlineString;
    c.CellReference = header + index;
    InlineString inlineString = new InlineString();
    DocumentFormat.OpenXml.Spreadsheet.Text t = new DocumentFormat.OpenXml.Spreadsheet.Text();
    t.Text = text;
    inlineString.AppendChild(t);
    c.AppendChild(inlineString);
    return c;
} 
Run Code Online (Sandbox Code Playgroud)

c# excel openxml spreadsheetml

16
推荐指数
2
解决办法
4万
查看次数

使用openxml将背景颜色应用于excel中的特定单元格

以下是我拥有的代码,在下面的最终方法中,我完成了所有操作。请忽略方法的返回类型,我后来改了。

public static byte[] CreateExcelDocument<T>(List<T> list, string filename)
        {
            DataSet ds = new DataSet();
            ds.Tables.Add(ListToDataTable(list));
            byte[] byteArray = CreateExcelDocumentAsStream(ds, filename);
            return byteArray;
        }
public static bool CreateExcelDocumentAsStream(DataSet ds, string filename, System.Web.HttpResponse Response)
        {
            try
            {
                System.IO.MemoryStream stream = new System.IO.MemoryStream();
                using (SpreadsheetDocument document = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook, true))
                {
                    WriteExcelFile(ds, document);
                }
                stream.Flush();
                stream.Position = 0;

                Response.ClearContent();
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "";

                //  NOTE: If you get an "HttpCacheability does not exist" error on the following line, make sure …
Run Code Online (Sandbox Code Playgroud)

.net c# excel openxml

7
推荐指数
1
解决办法
2万
查看次数

标签 统计

c# ×3

openxml ×3

excel ×2

.net ×1

openxml-sdk ×1

spreadsheetml ×1