Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

输出册记录预约信息 #1174

Open
DigitalPlatform opened this issue May 24, 2023 · 0 comments
Open

输出册记录预约信息 #1174

DigitalPlatform opened this issue May 24, 2023 · 0 comments

Comments

@DigitalPlatform
Copy link
Owner

output_reservations.cs

// 输出当前预约队列信息到 Excel 文件
// 2023/5/24

using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Xml;

using dp2Circulation;

using ClosedXML.Excel;

using DigitalPlatform;
using DigitalPlatform.IO;
using DigitalPlatform.Xml;
using DigitalPlatform.Text;

public class MyItemHost : ItemHost
{
    public override void OnInitial(object sender, StatisEventArgs e)
    {
        if (this.DbType != "item")
        {
            e.Continue = ContinueType.Error;
            e.ParamString = "本程序只能被 实体查询窗 所调用";
            return;
        }
    }

    string _fileName = "";
    XLWorkbook _doc = null;
    IXLWorksheet _sheet = null;
    int _row_index = 1;

    public override void OnBegin(object sender, StatisEventArgs e)
    {
        string strError = "";

        using (SaveFileDialog dlg = new SaveFileDialog())
        {
            // 询问文件名
            dlg.Title = "请指定要输出的 Excel 文件名";
            dlg.CreatePrompt = false;
            dlg.OverwritePrompt = true;
            dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            dlg.Filter = "Excel 文件 (*.xlsx)|*.xlsx|All files (*.*)|*.*";

            dlg.RestoreDirectory = true;

            if (dlg.ShowDialog(this.MainForm) != DialogResult.OK)
            {
                e.Continue = ContinueType.SkipAll;
                return;
            }

            _fileName = dlg.FileName;
        }


        try
        {
            _doc = new XLWorkbook(XLEventTracking.Disabled);
            File.Delete(_fileName);
        }
        catch (Exception ex)
        {
            strError = ExceptionUtil.GetAutoText(ex);
            goto ERROR1;
        }

        _sheet = _doc.Worksheets.Add("预约信息");

        // 输出到 Excel 文件
        int col_index = 1;
        WriteCell(_row_index, col_index++, "册条码号");
        WriteCell(_row_index, col_index++, "书名");
        WriteCell(_row_index, col_index++, "预约情况");
        _row_index++;
        return;
    ERROR1:
        e.ParamString = strError;
        e.Continue = ContinueType.Error;
    }

    public override void OnRecord(object sender, StatisEventArgs e)
    {
        string barcode = DomUtil.GetElementText(this.ItemDom.DocumentElement, "barcode");

        string location = DomUtil.GetElementText(this.ItemDom.DocumentElement, "location");
        location = StringUtil.GetPureLocationString(location);

        string parent = DomUtil.GetElementText(this.ItemDom.DocumentElement, "parent");

        string strItemDbName = Global.GetDbName(this.RecordPath);
        string strBiblioDbName = this.MainForm.GetBiblioDbNameFromItemDbName(strItemDbName);

        string biblio_recpath = strBiblioDbName + "/" + parent;

        var item_search_form = (ItemSearchForm)this.UiForm;

        // 输出书目信息
        // return:
        //      -1  出错
        //      0   没有找到
        //      1   找到
        int nRet = item_search_form.GetTable(
            biblio_recpath,
            "title,areas",
            out string strTableXml,
            out string strError);
        if (nRet == -1)
        {
            e.Continue = ContinueType.Error;
            e.ParamString = strError;
            return;
        }

        this.OutputText($"strTableXml={strTableXml}");


        var table = GetBiblioColumns(strTableXml);

        string title = (string)table["title"];
        this.OutputText($"title={title}");

        StringBuilder reseration_info = new StringBuilder();
        var nodes = this.ItemDom.DocumentElement.SelectNodes("reservations/request");
        int i = 0;
        foreach(XmlElement request in nodes)
        {
            string reader = request.GetAttribute("reader");
            string requestDate = request.GetAttribute("requestDate");
            requestDate = DateTimeUtil.LocalTime(requestDate);
            reseration_info.AppendLine($"{i+1}) {reader} {requestDate}");
            i++;
        }

        // 输出到 Excel 文件
        int col_index = 1;
        WriteCell(_row_index, col_index++, barcode);
        WriteCell(_row_index, col_index++, title);
        WriteCell(_row_index, col_index++, reseration_info.ToString());
        _row_index++;
    }

    void WriteCell(int nRowIndex, int nColIndex, string text)
    {
        IXLCell cell = _sheet.Cell(nRowIndex, nColIndex).SetValue(text);
        cell.Style.Alignment.WrapText = true;
        cell.Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
        /*
        cell.Style.Font.FontName = strFontName;
        nColIndex++;
        */
    }

    Hashtable GetBiblioColumns(string table_xml)
    {
        Hashtable results = new Hashtable();

        XmlDocument dom = new XmlDocument();
        dom.LoadXml(table_xml);
        XmlNodeList nodes = dom.DocumentElement.SelectNodes("line");
        foreach (XmlElement line in nodes)
        {
            string strName = line.GetAttribute("name");
            string strValue = line.GetAttribute("value");
            string strType = line.GetAttribute("type");

            if (strName == "_coverImage")
                continue;

            results[strType] = strValue;
        }
        return results;
    }

    public override void OnEnd(object sender, StatisEventArgs e)
    {
        CloseDoc();

        try
        {
            System.Diagnostics.Process.Start(_fileName);
        }
        catch
        {

        }
    }

    void CloseDoc()
    {
        if (_doc != null)
        {
            _doc.SaveAs(_fileName);
            _doc.Dispose();
            _doc = null;
        }
    }

    public override void FreeResources()
    {
        CloseDoc();
        base.FreeResources();
    }
}

output_reservations.cs.ref

<?xml version="1.0" encoding="utf-8"?>
<root>
  <ref>system.data.dll</ref>
  <ref>%bindir%/ClosedXml.dll</ref>
</root>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant