c#模块问题

使用问题 · 524 次浏览
侧耳倾听& 创建于 1天2小时前

//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   添加这个后还是不行

侧耳倾听& 最后更新于 2025/5/28

侧耳倾听& 1天1小时前 :

重新安装之前版本也不行了.0.0.

回复内容
CL 1天0小时前
#1

这个没有什么修改

侧耳倾听& 回复 CL 1天0小时前 :

老大能帮忙看下是什么原因吗,之前一直能正常运行。

获取文件保存者最后信息 - by 侧耳倾听& - 动作信息 - Quicker

随便用一个xlsx文档路径就行

侧耳倾听& 最后更新于 1天0小时前
CL 回复 侧耳倾听& 1天0小时前 :

它那个提示似乎要求你引用一下System.IO.Compression.dll 这个程序集。

侧耳倾听& 回复 CL 1天0小时前 :
我就是添加后还是不行.0.0.
侧耳倾听& 回复 侧耳倾听& 22小时26分钟前 :

已解决。

CL 回复 侧耳倾听& 17小时35分钟前 :

啥原因?

侧耳倾听& 回复 CL 2 分钟前 :
不清楚具体原因,改成下面引用就行了
//css_dir C:\Windows\Microsoft.NET\assembly\GAC_MSIL\** 
//css_ref System.IO.Compression.FileSystem.dll  
//css_ref NPOI.dll  
//css_ref NPOI.OOXML.dll  
//css_ref NPOI.HPSF.dll  
using System;
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;
回复主贴