How exactly do you propose to *control* Excel via an XML based file format? The one is a static file format, the other is an action that requires a dynamic state & some form of built in logic. idk...lets hope I'm totally misunderstanding you.
Some code I wrote to export table data... Uses openxml not the horrible interops.
You do need the openxml sdk installed on the target machine as well as excel.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
namespace Avis.RAC.TraficFines.ExcelHelper
{
public class Exporter
{
static string[] _headerColumns = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA"};
static int colCnt = 0;
public static string ExportData<T>(string fileName, IEnumerable<T> dataList)
{
if (dataList == null || dataList.Count() < 1)
{
return null;
}
List<string> excelRows = FormatListData<T>(dataList);
string finalFileName = CreateFile(fileName, excelRows);
return finalFileName;
}
/// <summary>
/// Reflects the name of the properties to act as the column headers for the spreadsheet
/// Reflects the values of the properties of T contained in the list to build values for spreadsheet
/// Creates a pipe delimited list of strings from the list of T
/// </summary>
private static List<string> FormatListData<T>(IEnumerable<T> dataList)
{
List<string> excelRows = new List<string>();
List<PropertyInfo> propInfo = null;
StringBuilder rowBuilder = new StringBuilder();
var header = dataList.First();
propInfo = header.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList();
colCnt = propInfo.Count;
foreach (var prop in propInfo)
{
string name = prop.Name;
rowBuilder.Append(name + '|');
}
excelRows.Add(rowBuilder.ToString());
rowBuilder.Clear();
foreach (var item in dataList)
{
propInfo = item.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList();
foreach (var prop in propInfo)
{
var value = prop.GetValue(item, null) ?? string.Empty;
rowBuilder.Append(value.ToString() + '|');
}
excelRows.Add(rowBuilder.ToString());
rowBuilder.Clear();
}
return excelRows;
}
private static string CreateFile(string fileName, List<string> excelRows)
{
try
{
string fullFileName = "Exports\\" + fileName + ".xlsx";
if (File.Exists(fullFileName))
{
File.Delete(fullFileName);
}
File.Copy("Exports\\ExportTemplate.xlsx", fullFileName);
using (SpreadsheetDocument workBook = SpreadsheetDocument.Open(fullFileName, true))
{
WorkbookPart workbookPart = workBook.WorkbookPart;
WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
int idx = 1;
foreach (var row in excelRows)
{
Row contentRow = CreateContentRow(idx, row);
idx++;
sheetData.AppendChild(contentRow);
}
}
return fullFileName;
}
catch (Exception ex)
{
throw ex;
}
}
private static Row CreateContentRow(int index, string row)
{
try
{
Row newRow = new Row();
newRow.RowIndex = (UInt32)index;
string[] bits = row.Split(new char[]{'|'}, StringSplitOptions.None);
for (int i = 0; i < bits.Length; i++)
{
if (i >= colCnt)
{
continue;
}
Cell cell = new Cell();
cell.DataType = CellValues.InlineString;
cell.CellReference = _headerColumns
+ index;
InlineString inlineString = new InlineString();
Text text = new Text { Text = bits.ToString() };
inlineString.AppendChild(text);
cell.AppendChild(inlineString);
newRow.AppendChild(cell);
}
return newRow;
}
catch (Exception ex)
{
throw ex;
}
}
}
}