新版本的xlsx是使用新的存储格式,貌似是处理过的XML。
对于OpenXML我网上搜了一下,很多人没有介绍。所以我就这里推荐下,相信会成为信息系统开发的必备。
先写出个例子,会发现如此的简介:
namespace XFormular.test
{
class Class1
{
public void test()
{
DataTable table = new DataTable("1");
table.Columns.Add("2");
for (int i = 0; i < 10; i++)
{
DataRow row = table.NewRow();
row[0] = i;
table.Rows.Add(row);
}
List<DataTable> lsit = new List<DataTable>();
lsit.Add(table);
OpenXmlSDKExporter.Export(AppDomain.CurrentDomain.BaseDirectory + "\\excel.xlsx", lsit);
}
}
}
</div>
写出代码
namespace XFormular
{
class OpenXmlSDKExporter
{
private static string[] Level = {"A", "B", "C", "D", "E", "F", "G",
"H", "I", "G", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z" };
public static List<DataTable> Import(string path)
{
List<DataTable> tables = new List<DataTable>();
if (path.EndsWith(ExcelHelper.POSTFIX_SVN))
return tables;
using (MemoryStream stream = SpreadsheetReader.StreamFromFile(path))
{
using (SpreadsheetDocument doc = SpreadsheetDocument.Open(stream, true))
{
foreach (Sheet sheet in doc.WorkbookPart.Workbook.Descendants<Sheet>())
{
DataTable table = new DataTable(sheet.Name.Value);
WorksheetPart worksheet = (WorksheetPart)doc.WorkbookPart.GetPartById(sheet.Id);
List<string> columnsNames = new List<string>();
foreach (Row row in worksheet.Worksheet.Descendants<Row>())
{
foreach (Cell cell in row)
{
string columnName = Regex.Match(cell.CellReference.Value, "[a-zA-Z]+").Value;
if (!columnsNames.Contains(columnName))
{
columnsNames.Add(columnName);
}
}
}
columnsNames.Sort(CompareColumn);
foreach (string columnName in columnsNames)
{
table.Columns.Add(columnName);
&