早上好, 我想从已有的excell文件中编辑一些单元格。我试过使用EPPlus和普通的OpenXml类。但是我失败了。在这两种情况下,程序不会崩溃,但总是返回旧的(未修改的)excel。请问我做错了什么?修改excel单元格
试验1 - EPPlus:
MemoryStream memoryStream = new MemoryStream();
using (var fs = new FileStream(@"Path\Test.xlsx", FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
{
memoryStream.Write(buffer, 0, bytesRead);
}
}
using (ExcelPackage excelPackage = new ExcelPackage(memoryStream))
{
ExcelWorkbook excelWorkBook = excelPackage.Workbook;
ExcelWorksheet excelWorksheet = excelWorkBook.Worksheets.First();
excelWorksheet.Cells[1, 1].Value = "Test";
excelWorksheet.Cells[3, 2].Value = "Test2";
excelWorksheet.Cells[3, 3].Value = "Test3";
excelPackage.Save();
}
memoryStream.Position = 0;
return new FileStreamResult(memoryStream, "application/xlsx")
{
FileDownloadName = "Tester.xlsx"
};
我怎么说,返回旧的Excel。但在调试模式下它也包含新的值。它看起来像memoryStream不能被修改。
试验2 - 处理OpenXML类
Stream stream = System.IO.File.Open(@"Path\Test.xlsx", FileMode.Open);
using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(stream, true))
{
WorksheetPart worksheetPart = GetWorksheetPartByName(spreadSheet, "Sheet1");
Cell cell = GetCell(worksheetPart.Worksheet, "C", 3);
cell.CellValue = new CellValue("Testos");
cell.DataType = new EnumValue<CellValues>(CellValues.String);
worksheetPart.Worksheet.Save();
}
stream.Position = 0;
return new FileStreamResult(stream, "application/xlsx")
{
FileDownloadName = "Tester.xlsx"
};
和辅助方法:
private static Row GetRow(Worksheet worksheet, uint rowIndex)
{
Row row;
if (worksheet.GetFirstChild<SheetData>().Elements<Row>().Where(r => r.RowIndex == rowIndex).Count() != 0)
{
row = worksheet.Elements<Row>().Where(r => r.RowIndex == rowIndex).FirstOrDefault();
}
else
{
row = new Row() { RowIndex = rowIndex };
worksheet.Append(row);
}
return row;
}
private static Cell GetCell(Worksheet worksheet, string columnName, uint rowIndex)
{
Row row = GetRow(worksheet, rowIndex);
string cellReference = columnName + rowIndex;
if (row.Elements<Cell>().Where(c => c.CellReference.Value == columnName + rowIndex).Count() > 0)
{
return row.Elements<Cell>().Where(c => c.CellReference.Value == cellReference).FirstOrDefault();
}
else
{
Cell refCell = null;
foreach (Cell cell in row.Elements<Cell>())
{
if (string.Compare(cell.CellReference.Value, cellReference, true) > 0)
{
refCell = cell;
break;
}
}
Cell newCell = new Cell() { CellReference = cellReference };
row.InsertBefore(newCell, refCell);
worksheet.Save();
return newCell;
}
}
private static WorksheetPart GetWorksheetPartByName(SpreadsheetDocument document, string sheetName)
{
IEnumerable<Sheet> sheets = document.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>().Where(s => s.Name == sheetName);
string relationshipId = sheets.First().Id.Value;
WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(relationshipId);
return worksheetPart;
}
再次感谢您的帮助。
它看起来非常好,它可以适用于流?由于我需要编辑本地Excel文件并将其返回下载。 (流和多路访问的可能性(更多的用户在时间)。谢谢。 – Ademar
随着Interop的使用,我不认为它可能与内存流工作。建议:http://www.codeproject.com/Questions/623916/Load-excel-workbook-from-memory-stream考虑将文件写入临时文件夹,然后将其作为内存流返回给用户 –