新版本的xlsx是使用新的存储格式,貌似是处理过的XML。

传统的excel处理方法,我真的感觉像屎。用Oldeb不方便,用com组件要实际调用excel打开关闭,很容易出现死。

对于OpenXML我网上搜了一下,很多人没有介绍。所以我就这里推荐下,相信会成为信息系统开发的必备。

先写出个例子,会发现如此的简介:

using System;
using System.Collections.Generic;
using System.Text;
using XFormular.config;
using System.IO;
using com.xtar.amfx;
using System.Runtime.Serialization.Formatters.Binary;
using System.Data; namespace XFormular.test
{
class Class1
{
public void test()
{
DataTable table = new DataTable("");
table.Columns.Add("");
for (int i = ; i < ; i++)
{
DataRow row = table.NewRow();
row[] = i;
table.Rows.Add(row);
} List<DataTable> lsit = new List<DataTable>();
lsit.Add(table); OpenXmlSDKExporter.Export(AppDomain.CurrentDomain.BaseDirectory + "\\excel.xlsx", lsit);
}
}
}

写出代码:

using System;
using System.IO;
using System.Windows.Forms;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Extensions;
using System.Collections.Generic;
using System.Data;
using System.Text.RegularExpressions; namespace XFormular
{
//http://simpleooxml.codeplex.com/
//http://www.pin5i.com/showtopic-21817.html
//http://blog.csdn.net/lbj147123/article/details/6603942
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);
} foreach (Row row in worksheet.Worksheet.Descendants<Row>())
{
DataRow tableRow = table.NewRow();
table.Rows.Add(tableRow); foreach (Cell cell in row)
{
string columnName = Regex.Match(cell.CellReference.Value, "[a-zA-Z]+").Value;
tableRow[columnName] = GetValue(cell, doc.WorkbookPart.SharedStringTablePart);
}
} if (table.Rows.Count <= )
continue;
if (table.Columns.Count <= )
continue; tables.Add(table);
}
}
} return tables;
} public static String GetValue(Cell cell, SharedStringTablePart stringTablePart)
{ if (cell.ChildElements.Count == ) return null; //get cell value String value = cell.CellValue.InnerText; //Look up real value from shared string table if ((cell.DataType != null) && (cell.DataType == CellValues.SharedString)) value = stringTablePart.SharedStringTable .ChildElements[Int32.Parse(value)] .InnerText; return value; } public static void Export(string path, List<DataTable> tables)
{
using (MemoryStream stream = SpreadsheetReader.Create())
{
using (SpreadsheetDocument doc = SpreadsheetDocument.Open(stream, true))
{
SpreadsheetWriter.RemoveWorksheet(doc, "Sheet1");
SpreadsheetWriter.RemoveWorksheet(doc, "Sheet2");
SpreadsheetWriter.RemoveWorksheet(doc, "Sheet3"); foreach (DataTable table in tables)
{
WorksheetPart sheet = SpreadsheetWriter.InsertWorksheet(doc, table.TableName);
WorksheetWriter writer = new WorksheetWriter(doc, sheet); SpreadsheetStyle style = SpreadsheetStyle.GetDefault(doc); foreach (DataRow row in table.Rows)
{
for (int i = ; i < table.Columns.Count; i++)
{
string columnName = SpreadsheetReader.GetColumnName("A", i);
string location = columnName + (table.Rows.IndexOf(row) + );
writer.PasteText(location, row[i].ToString(), style);
}
} writer.Save();
}
SpreadsheetWriter.StreamToFile(path, stream);//保存到文件中
}
}
} private static int CompareColumn(string x, string y)
{
int xIndex = Letter_to_num(x);
int yIndex = Letter_to_num(y);
return xIndex.CompareTo(yIndex);
} /// <summary>
/// 数字26进制,转换成字母,用递归算法
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private static string Num_to_letter(int value)
{
//此处判断输入的是否是正确的数字,略(正在表达式判断)
int remainder = value % ;
//remainder = (remainder == 0) ? 26 : remainder;
int front = (value - remainder) / ;
if (front < )
{
return Level[front - ] + Level[remainder];
}
else
{
return Num_to_letter(front) + Level[remainder];
}
//return "";
} /// <summary>
/// 26进制字母转换成数字
/// </summary>
/// <param name="letter"></param>
/// <returns></returns>
private static int Letter_to_num(string str)
{
//此处判断是否是由A-Z字母组成的字符串,略(正在表达式片段)
char[] letter = str.ToCharArray(); //拆分字符串
int reNum = ;
int power = ; //用于次方算值
int times = ; //最高位需要加1
int num = letter.Length;//得到字符串个数
//得到最后一个字母的尾数值
reNum += Char_num(letter[num - ]);
//得到除最后一个字母的所以值,多于两位才执行这个函数
if (num >= )
{
for (int i = num - ; i > ; i--)
{
power = ;//致1,用于下一次循环使用次方计算
for (int j = ; j < i; j++) //幂,j次方,应该有函数
{
power *= ;
}
reNum += (power * (Char_num(letter[num - i - ]) + times)); //最高位需要加1,中间位数不需要加一
times = ;
}
}
//Console.WriteLine(letter.Length);
return reNum;
} /// <summary>
/// 输入字符得到相应的数字,这是最笨的方法,还可用ASIICK编码;
/// </summary>
/// <param name="ch"></param>
/// <returns></returns>
private static int Char_num(char ch)
{
switch (ch)
{
case 'A':
return ;
case 'B':
return ;
case 'C':
return ;
case 'D':
return ;
case 'E':
return ;
case 'F':
return ;
case 'G':
return ;
case 'H':
return ;
case 'I':
return ;
case 'J':
return ;
case 'K':
return ;
case 'L':
return ;
case 'M':
return ;
case 'N':
return ;
case 'O':
return ;
case 'P':
return ;
case 'Q':
return ;
case 'R':
return ;
case 'S':
return ;
case 'T':
return ;
case 'U':
return ;
case 'V':
return ;
case 'W':
return ;
case 'X':
return ;
case 'Y':
return ;
case 'Z':
return ;
}
return -;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb; namespace xtar_biz_codegen
{
class ExcelHelper
{
public static string POSTFIX_97 = "XLS"; public static string POSTFIX_03 = "XLSX";
}
}

最后附送一个项目文件,可以自己测试:

http://pan.baidu.com/s/1msFuY

一个用微软官方的OpenXml读写Excel 目前网上不太普及的方法。的更多相关文章

  1. #应用openxml读写excel代码

    这个例子比较简单,没有考虑格式之类的问题. using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Spreadshe ...

  2. Python: 读写Excel(openpyxl / win32com.client)

    项目周报汇报的时候要做数据汇总,总是要从不同的excel文件中去获取数据最后汇总到一个excel表里面,所以决定用python直接写个自动化脚本来自动执行. 用python来读写excel,目前找了2 ...

  3. 微软官方的Excel android 移动版的折腾

    微软官方的Excel android 移动版,有重大bug.害我折腾了一天多时间.最终确认是Excel自身的问题. 现象描述:手机上新建或是保存excel后.放到电脑上,不能打开.提示”Excel在B ...

  4. 微软官方 Win 11 “体检工具”太烂了?开发者自己做了一个

    1.Win 10 免费升级到 Win 11 最近微软官方终于宣布了 Windows 11,不仅带来了全新的 UI,而且还有很多新功能:比如支持 Android 应用. 虽然微软官方已说明 Win 10 ...

  5. 【原创】.NET读写Excel工具Spire.Xls使用(1)入门介绍

    在.NET平台,操作Excel文件是一个非常常用的需求,目前比较常规的方法有以下几种: 1.Office Com组件的方式:这个方式非常累人,微软的东西总是这么的复杂,使用起来可能非常不便,需要安装E ...

  6. 【原创】.NET读写Excel工具Spire.Xls使用(3)单元格控制

                  本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p/4288836.html .NET读写Excel工具Spire.Xls使用文章 ...

  7. .NET读写Excel工具Spire.Xls使用(1)入门介绍

    原文:[原创].NET读写Excel工具Spire.Xls使用(1)入门介绍 在.NET平台,操作Excel文件是一个非常常用的需求,目前比较常规的方法有以下几种: 1.Office Com组件的方式 ...

  8. 使用NPOI读写Excel、Word

    NPOI 是 POI 项目的 .NET 版本.POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目. 使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 ...

  9. 【原创】.NET读写Excel工具Spire.Xls使用(4)对数据操作与控制

                  本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p/4288836.html .NET读写Excel工具Spire.Xls使用文章 ...

随机推荐

  1. db2设置共享内存

    db2 UPDATE DBM CFG USING INSTANCE_MEMORY 5242880 IMMEDIATEdb2 UPDATE DBM CFG USING INSTANCE_MEMORY A ...

  2. 数据存储--沙盒sandBox

    默认情况下,每个沙盒必含有3个文件夹:Documents, Library 和 tmp 一.沙盒(sandbox)出于安全的目的,应用程序只能将自己的数据和偏好设置写入到几个特定的位置上.当应用程序被 ...

  3. win7/win8远程桌面 server 2003 卡的问题

    原因在于从vista开始,微软在TCP/IP协议栈里新加了一个叫做“Window Auto-Tuning”的功能.这个功能本身的目的是为了让操作系统根据网络的实时性能(比如响应时间)来动态调整网络上传 ...

  4. javascript typeof

    https://zhidao.baidu.com/question/79159257.html typeof 运算符返回一个用来表示表达式的数据类型的字符串. 可能的字符串有:"number ...

  5. 编译gtk+程序报错gcc: pkg-config --cflags --libs gtk+-2.0: 没有那个文件或目录

    第一次接触gtk+.在网上搜罗良一番,装好相应的库后,编写了第一hello程序.在编译时输入以下命令:gcc -o hello hello.c 'pkg-config --cflags --libs ...

  6. android studio 使用 jni 编译 opencv 完整实例 之 图像边缘检测!

    目录: 1,过程感慨: 2,运行环境: 3,准备工作: 4,编译 .so 5,遇到的关键问题及其解决方法 6,实现效果截图. ------------------------------------- ...

  7. 老毛桃u盘装系统制作工具

    老毛桃[url=http://www.laomaotao.cn.com/]一键u盘装系统下载[/url]告别繁琐,简单易用,一盘两用,携带方便.不需要任何技术基础,一键制作,自动完成制作,平时当U盘使 ...

  8. 「2014-4-13」Think twice before starting the adventure

    杂文一篇. 1. 取名字真心是一件特别困难的事情.这位独立开发者花了将近两天的时间,给他的私人项目取了个名字:这篇博客<为何我不鸟你的开源项目>里显然还忽视了一个原因,就是名字取得太烂以至 ...

  9. 8.2 辅助 xUtils 3.0

    主要有四大模块: DbUtils模块: android中的orm(对象关系映射)框架,一行代码就可以进行增删改查: 支持事务,默认关闭: 可通过注解自定义表名,列名,外键,唯一性约束,NOT NULL ...

  10. 【随记】Hello World小记

    今天装Python,如下: 突然想到,到现在,我已经数不清写过多少遍Hello World了. 最早是初一学VB的时候,用Label1在Form1上画一个,然后修改Caption属性为“Hello W ...