一开始在读取ET文件的时候用的是etapi.dll自带插件,发现调试时会报各种各样的拒绝访问的错误,创建txt文件报【对路径“c:\\windows\\system32\\inetsrv\\sss.txt"的访问被拒绝】(安装IIS的路径),或报{检索COM类工厂中CLSID为{00024500-0000-0000-C000-000000000046}组件失败,原因是出现以下错误:80070005拒绝访问。。。感觉用这个插件实现不太如愿,换另一个NPOI插件!
解析ET文件使用的是NPOI,它是开源的POI项目的.NET 版,在处理Excel文件兼容xls和xlsx,不过不知道兼不兼容ET文件的不同版本,这个待考证。
NPOI使用HSSFWork类来处理xls,XSSFWorkbook类来处理xlsx,它们都继承自IWorkbook。
上传ET文件使用的是项目特有框架传到后台进行编辑,此时从后台解析ET文件开始(WPS Excel文件简称ET文件)。。。
private int Do(Context operationContext) {
try{
if(uploadFile != null && uploadFile.InputStream.Length > 0)
{
string excelFileName = Path.GetExtension(uploadFile.FileName).ToLower();
if (!Regex.Match(excelFileName,".(xls|xlsx|et)").Success)
{
thorw new EBException("","非【Excel】文档!");
}
if (uploadFile.InputStream.Length > 500*1024*1024)
{
thorw new EBException("","文档大小不能超过5MBC# 上传WPS Excel 后台转换成txt,再压缩为ZIP!");
}
}
// 文件长度
int len = (int)uploadFile.InputStream.Length;
byte[] buffer = new byte[len];
HSSFWorkbook hssf = newHSSFWorkbook(uploadFile.InputStream);
ISheet sheet = hssf.GetSheetAt(0);
string info = "";
// 从第二行开始解析数据
for (int i = 1 ;i <= sheet.LastRowNum; i++) {
IRow row = sheet.GetRow(i);
if(row == null)
{
break;
}
string value0 = "",value1="",value2="";
value0=GetCellValue(row.Cell[0]);
value1=GetCellValue(row.Cell[1]);
value2=GetCellValue(row.Cell[2]);
if(!string.IsNullOrEmpty(value0) || !string.IsNullOrEmpty(value1) || !string.IsNullOrEmpty(value2)){
if(string.IsNullOrEmpty(value0)){
throw new EBException("","第"+(i + 1) + "行客户号为空值!");
}
if(value0.Trim().Length > 16){
throw new EBException("","第"+(i + 1) + "行客户号的长度不超过16位!");
}
。
。
。
//校验
}else {
break;
}
}
// 将读取出来的数据格式编辑
info += value0 +"|" + value1 + "\r\n"; if(info != null && !"".Equals(info.Trim())){
buffer = Encoding.UTF8.GetBytes(info);
} else { throw new EBException("上传的文件为空"); }
// 将编辑好的文压缩 byte[] buff =ZipOutputStream(buffer,"sss.txt"); return ok;
}
catch(Exception ex){
Console.WriteLine("调用失败");
} finally {
if (uploadFile != null && uploadFile.InputStream != null) {
uploadFile.InputStream.Close();
}
}
// 将TXT以流的形式压缩,再传递或再编辑,这里以内存流的形式进行操作的,用到的插件是ICSharpCode.SharpZipLib.dll
public static byte[] ZipOutputStream(byte[] bytes,string name){
//内存流
using(MemoryStream ms = MemoryStream()) {
try{
// 创建ZIP文件
//这里在创建压缩文件流时必须用using给括起来,不知道为啥,否则压缩文件ZIP格式会报错
//用using可以不用手动释放,。。
using(ZipOutputStream zout = new ZipOutputStream(ms)){
zout.UseZip64 = UseZip6.Off;
//将编辑好的字节流添加 到ZIP包中
zout.PutNextEntry(new ZipEntry(name));
zout.Write(bytes, 0, bytes.length);
}
byte[] zipBuffer = ms.toArrya();
return zipBuffer;
}
}
private string GetCellValue(ICell cell)
{
if(cell == null){
return string.Empty;
}
switch (cell.CellType)
{
case CellType.Blank:
return string.Empty;
case CellType.Boolean:
return cell.BooleanCellValue.ToString();
case CellType.Error:
return cell.ErrorCellValue.ToString();
case CellType.String:
return cell.StringCellValue();
case CellType.Formula:
try{
HSSFFormulaEvaluator e = new HSSFFormulaEvaluator(cell.Sheet.Workbook);
e.EvaluateInCell(cell);
return cell.ToString();
}
catch {
return cell.NumericCellValue.ToString();
}
case CellType.Numeric:
case CellType.Unknown:
default:
return cell.ToString();
}
}
}
部分代码参照:/zhouqinghe24/article/details/8649346
/lazyneal/p/6148912.html
如果觉得《C# 上传WPS Excel 后台转换成txt 再压缩为ZIP》对你有帮助,请点赞、收藏,并留下你的观点哦!