//css_ref System.IO.Compression.FileSystem.dll
//css_ref NPOI.dll
//css_ref NPOI.OOXML.dll
//css_ref NPOI.HPSF.dll
using System.IO
using NPOI.HPSF;
using NPOI.HSSF.UserModel;
using System.Windows.Forms;
using System.IO.Compression;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
// Quicker将会调用的函数。可以根据需要修改返回值类型。
public static List<string> Exec(Quicker.Public.IStepContext context)
{
var oldValue = (List<string>)context.GetVarValue("list"); // 读取动作里的变量值
//MessageBox.Show(oldValue as string);
//context.SetVarValue("varName", "从脚本输出的内容。"); // 向变量里输出值
//MessageBox.Show("Hello World!");
return oldValue.AsParallel().AsOrdered().Select(x=> string.IsNullOrWhiteSpace(x) ? "" : x.EndsWith(".xls",StringComparison.OrdinalIgnoreCase) ? GetXlsMetadata(x) : GetXlsxMetadata(x)).ToList();
}
public static string GetXlsxMetadata(string filePath)
{
try
{
using (ZipArchive zip = ZipFile.OpenRead(filePath))
{
var coreEntry = zip.GetEntry("docProps/core.xml");
using (var stream = coreEntry.Open())
using (var reader = new StreamReader(stream))
{
string xml = reader.ReadToEnd();
string author = Regex.Match(xml, @"<dc:creator>(.*?)</dc:creator>").Groups[1].Value;
string lastModifiedBy = Regex.Match(xml, @"<cp:lastModifiedBy>(.*?)</cp:lastModifiedBy>").Groups[1].Value;
// 解析XML获取<cp:lastModifiedBy>节点
//MessageBox.Show(xml);
return author+"\t"+lastModifiedBy;
}
}
}
catch (Exception ex)
{
//MessageBox.Show($"错误: {ex.Message}");
return "错误";
}
}
public static string GetXlsMetadata(string filePath)
{
try
{
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
var workbook = new HSSFWorkbook(fs);
var si = workbook.SummaryInformation;
var dsi = workbook.DocumentSummaryInformation;
// 作者信息(强制转换字节数据)
string author = "";
var authorObj = si?.GetProperty(0x0004);
if (authorObj is byte[] authorBytes)
author = DecodeWithFallback(authorBytes);
else
author = authorObj?.ToString() ?? "";
// 最后保存者(优先处理二进制数据)
string lastAuthor = "";
if (dsi != null)
{
var lastAuthorObj = dsi.GetProperty(0x0008) ?? si?.GetProperty(0x0008);
if (lastAuthorObj is byte[] bytes)
lastAuthor = DecodeWithFallback(bytes);
else
lastAuthor = lastAuthorObj?.ToString() ?? "";
}
return $"{author}\t{lastAuthor}";
}
}
catch (Exception ex)
{
//Debug.WriteLine($"元数据读取失败: {ex.Message}");
return "ERROR";
}
}
// 添加智能编码检测方法
private static string DecodeWithFallback(byte[] bytes)
{
Encoding[] encodings = {
Encoding.GetEncoding(936), // GBK优先
Encoding.GetEncoding(950), // 兼容繁体Big5
Encoding.UTF8, // 特殊场景UTF8
Encoding.GetEncoding(1252) // 最终回退
};
foreach (var enc in encodings)
{
try
{
return enc.GetString(bytes);
}
catch { }
}
return Encoding.Default.GetString(bytes);
}
上面是之前代码,能直接运行
编译失败, Compilation, (56,13): error CS1069: The type name 'ZipArchive' could not be found in the namespace 'System.IO.Compression'. This type has been forwarded to assembly 'System.IO.Compression, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' Consider adding a reference to that assembly.
(56,30): error CS0103: The name 'ZipFile' does not exist in the current context
更新后提示这个,老大是最近哪里修改过吗
用的 普通模式v2 (Roslyn)
//css_ref System.IO.Compression.dll 添加这个后还是不行
老大能帮忙看下是什么原因吗,之前一直能正常运行。
获取文件保存者最后信息 - by 侧耳倾听& - 动作信息 - Quicker
随便用一个xlsx文档路径就行
重新安装之前版本也不行了.0.0.