using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
public class ExcelExport : IDisposable
{
int rowCount = 0;
string sheetName = "sheet1";
SpreadsheetDocument xl;
OpenXmlWriter oxw;
WorksheetPart wsp;
///
/// 导出Excel
///
public ExcelExport(string path, int rowCount, string sheetName = "sheet1")
{
this.rowCount = rowCount;
this.sheetName = sheetName ?? "sheet1";
xl = SpreadsheetDocument.Create(path, SpreadsheetDocumentType.Workbook);
xl.AddWorkbookPart();
wsp = xl.WorkbookPart.AddNewPart();
oxw = OpenXmlWriter.Create(wsp);
oxw.WriteStartElement(new Worksheet());
oxw.WriteStartElement(new SheetData());
}
///
/// 写入表格数据
///
public void Write(object[] datas)
{
if (datas == null || datas.Length == 0) return;
int colNum = datas.Length;
//oxa = new List();
// this is the row index
//oxa.Add(new OpenXmlAttribute("r", null, i.ToString()));
//oxw.WriteStartElement(new Row(), oxa);
oxw.WriteStartElement(new Row());
for (int j = 0; j < colNum; ++j)
{
var oxa = new List();
// this is the data type ("t"), with CellValues.String ("str")
oxa.Add(new OpenXmlAttribute("t", null, "str"));
// it's suggested you also have the cell reference, but
// you'll have to calculate the correct cell reference yourself.
// Here's an example:
//oxa.Add(new OpenXmlAttribute("r", null, "A1"));
//c.Append(f);
oxw.WriteStartElement(new Cell() { DataType = CellValues.InlineString }, oxa);
//oxw.WriteStartElement(new Cell());
oxw.WriteElement(new CellValue($"{datas[j]}"));
// this is for Cell
oxw.WriteEndElement();
}
// this is for Row
oxw.WriteEndElement();
}
///
/// 写入表格数据
///
public void Write(List