Exemple #1
0
        // return:
        //      -1  出错
        //      0   用户中断
        //      1   成功
        public int CreateReaderDetailExcelFile(List<string> reader_barcodes,
            bool bLaunchExcel,
            out string strError)
        {
            strError = "";
            //int nRet = 0;

            ExportPatronExcelDialog dlg = new ExportPatronExcelDialog();
            MainForm.SetControlFont(dlg, this.Font, false);
            dlg.OverwritePrompt = true;
            dlg.UiState = this.MainForm.AppInfo.GetString(
        "ReaderSearchForm",
        "ExportPatronExcelDialog_uiState",
        "");

            this.MainForm.AppInfo.LinkFormState(dlg, "ReaderSearchForm_ExportPatronExcelDialog_uiState_state");
            dlg.ShowDialog(this);

            this.MainForm.AppInfo.SetString(
"ReaderSearchForm",
"ExportPatronExcelDialog_uiState",
dlg.UiState);

            if (dlg.DialogResult == System.Windows.Forms.DialogResult.Cancel)
            {
                strError = "放弃操作";
                return 0;
            }

#if NO
            // 提醒覆盖文件
            if (File.Exists(dlg.FileName) == true)
            {
                DialogResult result = MessageBox.Show(this,
                    "文件 '" + dlg.FileName + "' 已经存在。继续操作将覆盖此文件。\r\n\r\n请问是否要覆盖此文件? (OK 覆盖;Cancel 放弃操作)",
                    "ReaderSearchForm",
                    MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Question,
                    MessageBoxDefaultButton.Button1);
                if (result == DialogResult.Cancel)
                {
                    strError = "放弃操作";
                    return 0;
                }
            }
#endif

            string strTimeRange = "";

            try
            {
                strTimeRange = GetTimeRange(dlg.ChargingHistoryDateRange);
            }
            catch (Exception ex)
            {
                strError = "日期范围字符串 '" + dlg.ChargingHistoryDateRange + "' 格式不合法: " + ex.Message;
                return -1;
            }

#if NO
            // 询问文件名
            SaveFileDialog dlg = new SaveFileDialog();

            dlg.Title = "请指定要输出的 Excel 文件名";
            dlg.CreatePrompt = false;
            dlg.OverwritePrompt = true;
            // dlg.FileName = this.ExportExcelFilename;
            // dlg.InitialDirectory = Environment.CurrentDirectory;
            dlg.Filter = "Excel 文件 (*.xlsx)|*.xlsx|All files (*.*)|*.*";

            dlg.RestoreDirectory = true;

            if (dlg.ShowDialog() != DialogResult.OK)
                return 0;
#endif

            XLWorkbook doc = null;
            try
            {
                doc = new XLWorkbook(XLEventTracking.Disabled);
                File.Delete(dlg.FileName);
            }
            catch (Exception ex)
            {
                strError = "ReaderSearchForm new XLWorkbook() {39D0940F-33FF-4A10-8F61-1FFFEEBFF4D0} exception: " + ExceptionUtil.GetAutoText(ex);
                return -1;
            }

            IXLWorksheet sheet = null;
            sheet = doc.Worksheets.Add("表格");

            // TODO: sheet 可以按照单位来区分。例如按照班级

            stop.Style = StopStyle.EnableHalfStop;
            stop.OnStop += new StopEventHandler(this.DoStop);
            stop.Initial("导出读者详情 ...");
            stop.BeginLoop();

            EnableControls(false);
            this.listView_records.Enabled = false;

            try
            {
                if (stop != null)
                    stop.SetProgressRange(0, reader_barcodes.Count);

                // 每个列的最大字符数
                List<int> column_max_chars = new List<int>();

                // TODO: 表的标题,创建时间

                int nRowIndex = 3;  // 空出前两行
                //int nColIndex = 1;

                int nReaderIndex = 0;
                foreach (string strBarcode in reader_barcodes)
                {
                    Application.DoEvents();	// 出让界面控制权

                    if (stop != null && stop.State != 0)
                    {
                        strError = "用户中断";
                        return 0;
                    }

                    if (string.IsNullOrEmpty(strBarcode) == true)
                        continue;

                    // 获得读者记录
                    byte[] baTimestamp = null;
                    string strOutputRecPath = "";

                    stop.SetMessage("正在处理读者记录 " + strBarcode + " ...");

                    string[] results = null;
                    long lRet = Channel.GetReaderInfo(
                        stop,
                        strBarcode,
                        "advancexml,advancexml_borrow_bibliosummary,advancexml_overdue_bibliosummary", // advancexml_history_bibliosummary
                        out results,
                        out strOutputRecPath,
                        out baTimestamp,
                        out strError);
                    if (lRet == -1)
                        return -1;

                    if (lRet == 0)
                        return -1;

                    if (lRet > 1)   // 不可能发生吧?
                    {
                        strError = "读者证条码号 " + strBarcode + " 命中记录 " + lRet.ToString() + " 条,放弃装入读者记录。\r\n\r\n注意这是一个严重错误,请系统管理员尽快排除。";
                        return -1;
                    }
                    if (results == null || results.Length < 1)
                    {
                        strError = "返回的results不正常。";
                        return -1;
                    }
                    string strXml = results[0];

                    XmlDocument dom = new XmlDocument();
                    try
                    {
                        dom.LoadXml(strXml);
                    }
                    catch (Exception ex)
                    {
                        strError = "装载读者记录 XML 到 DOM 时发生错误: " + ex.Message;
                        return -1;
                    }

                    // 
                    if (dlg.ExportReaderInfo)
                    {
                        OutputReaderInfo(sheet,
                dom,
                nReaderIndex,
                ref nRowIndex,
                ref column_max_chars);
                    }

                    // 输出在借册表格
                    if (dlg.ExportBorrowInfo)
                    {
                        OutputBorrows(sheet,
                dom,
                this.MainForm.GetBiblioSummary,
                true,
                ref nRowIndex,
                ref column_max_chars);
                    }

                    // 输出违约金表格
                    if (dlg.ExportOverdueInfo)
                    {
                        OutputOverdues(sheet,
                dom,
                this.MainForm.GetBiblioSummary,
                ref nRowIndex,
                ref column_max_chars);
                    }

                    if (dlg.ExportChargingHistory)
                    {
                        try
                        {
                            ChargingHistoryLoader history_loader = new ChargingHistoryLoader();
                            history_loader.Channel = this.Channel;
                            history_loader.Stop = this.stop;
                            history_loader.PatronBarcode = strBarcode;
                            history_loader.TimeRange = strTimeRange;
                            history_loader.Actions = "return,lost";
                            history_loader.Order = "descending";

                            CacheableBiblioLoader summary_loader = new CacheableBiblioLoader();
                            summary_loader.Channel = this.Channel;
                            summary_loader.Stop = this.stop;
                            summary_loader.Format = "summary";
                            summary_loader.GetBiblioInfoStyle = GetBiblioInfoStyle.None;
                            // summary_loader.RecPaths = biblio_recpaths;

                            // 输出借阅历史表格
                            OutputBorrowHistory(sheet,
                    dom,
                    history_loader,
                                // this.MainForm.GetBiblioSummary,
                    summary_loader,
                    ref nRowIndex,
                    ref column_max_chars);
                        }
                        catch (Exception ex)
                        {
                            strError = "输出借阅历史时出现异常: " + ex.Message;
                            return -1;
                        }
                    }

                    nRowIndex++;    // 读者之间的空行

                    nReaderIndex++;
                    if (stop != null)
                        stop.SetProgressValue(nReaderIndex);
                }

                {
                    if (stop != null)
                        stop.SetMessage("正在调整列宽度 ...");
                    Application.DoEvents();

                    //double char_width = GetAverageCharPixelWidth(list);

                    // 字符数太多的列不要做 width auto adjust
                    foreach (IXLColumn column in sheet.Columns())
                    {
                        int MAX_CHARS = 50;   // 60

                        int nIndex = column.FirstCell().Address.ColumnNumber - 1;
                        if (nIndex >= column_max_chars.Count)
                            break;
                        int nChars = column_max_chars[nIndex];

                        if (nIndex == 1)
                        {
                            column.Width = 10;
                            continue;
                        }

                        if (nIndex == 3)
                            MAX_CHARS = 50;
                        else
                            MAX_CHARS = 24;

                        if (nChars < MAX_CHARS)
                            column.AdjustToContents();
                        else
                            column.Width = Math.Min(MAX_CHARS, nChars);

                        //else
                        //    column.Width = (double)list.Columns[i].Width / char_width;  // Math.Min(MAX_CHARS, nChars);
                    }
                }
            }
            finally
            {
                EnableControls(true);
                this.listView_records.Enabled = true;

                stop.EndLoop();
                stop.OnStop -= new StopEventHandler(this.DoStop);
                stop.Initial("");
                stop.HideProgress();
                stop.Style = StopStyle.None;

                if (doc != null)
                {
                    doc.SaveAs(dlg.FileName);
                    doc.Dispose();
                }

                if (bLaunchExcel)
                {
                    try
                    {
                        System.Diagnostics.Process.Start(dlg.FileName);
                    }
                    catch
                    {

                    }
                }

            }
            return 1;
        }
Exemple #2
0
        // return:
        //      -1  出错
        //      0   用户中断
        //      1   成功
        public int CreateReaderDetailExcelFile(List<string> reader_barcodes,
            bool bLaunchExcel,
            out string strError)
        {
            strError = "";
            //int nRet = 0;

            ExportPatronExcelDialog dlg = new ExportPatronExcelDialog();
            MainForm.SetControlFont(dlg, this.Font, false);
            dlg.OverwritePrompt = true;
            dlg.UiState = this.MainForm.AppInfo.GetString(
        "ReaderSearchForm",
        "ExportPatronExcelDialog_uiState",
        "");

            this.MainForm.AppInfo.LinkFormState(dlg, "ReaderSearchForm_ExportPatronExcelDialog_uiState_state");
            dlg.ShowDialog(this);

            this.MainForm.AppInfo.SetString(
"ReaderSearchForm",
"ExportPatronExcelDialog_uiState",
dlg.UiState);

            if (dlg.DialogResult == System.Windows.Forms.DialogResult.Cancel)
            {
                strError = "放弃操作";
                return 0;
            }

            string strTimeRange = "";

            try
            {
                strTimeRange = GetTimeRange(dlg.ChargingHistoryDateRange);
            }
            catch (Exception ex)
            {
                strError = "日期范围字符串 '" + dlg.ChargingHistoryDateRange + "' 格式不合法: " + ex.Message;
                return -1;
            }

            XLWorkbook doc = null;
            try
            {
                doc = new XLWorkbook(XLEventTracking.Disabled);
                File.Delete(dlg.FileName);
            }
            catch (Exception ex)
            {
                strError = "ReaderSearchForm new XLWorkbook() {39D0940F-33FF-4A10-8F61-1FFFEEBFF4D0} exception: " + ExceptionUtil.GetAutoText(ex);
                return -1;
            }

            IXLWorksheet sheet = null;
            sheet = doc.Worksheets.Add("表格");

            // 每个列的最大字符数
            List<int> column_max_chars = new List<int>();

            // TODO: 表的标题,创建时间

            int nRowIndex = 3;  // 空出前两行
            //int nColIndex = 1;

            int nReaderIndex = 0;

            try
            {
                // return:
                //      -1  出错。包括用户中断的情况
                //      >=0 实际处理的读者记录数
                int nRet = this.ProcessPatrons(
                    reader_barcodes,
                    "advancexml,advancexml_borrow_bibliosummary,advancexml_overdue_bibliosummary", // advancexml_history_bibliosummary
                    (strRecPath, dom, timestamp) =>
                    {
                        this.ShowMessage("正在处理读者记录 " + strRecPath);

                        string strBarcode = DomUtil.GetElementText(dom.DocumentElement, "barcode");

                        // 
                        if (dlg.ExportReaderInfo)
                        {
                            OutputReaderInfo(sheet,
                    dom,
                    nReaderIndex,
                    ref nRowIndex,
                    ref column_max_chars);
                        }

                        // 输出在借册表格
                        if (dlg.ExportBorrowInfo)
                        {
                            OutputBorrows(sheet,
                    dom,
                    this.MainForm.GetBiblioSummary,
                    true,
                    ref nRowIndex,
                    ref column_max_chars);
                        }

                        // 输出违约金表格
                        if (dlg.ExportOverdueInfo)
                        {
                            OutputOverdues(sheet,
                    dom,
                    this.MainForm.GetBiblioSummary,
                    ref nRowIndex,
                    ref column_max_chars);
                        }

                        if (dlg.ExportChargingHistory)
                        {
                            try
                            {
                                ChargingHistoryLoader history_loader = new ChargingHistoryLoader();
                                history_loader.Channel = this.Channel;
                                history_loader.Stop = this.stop;
                                history_loader.PatronBarcode = strBarcode;
                                history_loader.TimeRange = strTimeRange;
                                history_loader.Actions = "return,lost";
                                history_loader.Order = "descending";

                                CacheableBiblioLoader summary_loader = new CacheableBiblioLoader();
                                summary_loader.Channel = this.Channel;
                                summary_loader.Stop = this.stop;
                                summary_loader.Format = "summary";
                                summary_loader.GetBiblioInfoStyle = GetBiblioInfoStyle.None;
                                // summary_loader.RecPaths = biblio_recpaths;

                                // 输出借阅历史表格
                                // 可能会抛出异常,例如权限不够
                                OutputBorrowHistory(sheet,
                        dom,
                        history_loader,
                                    // this.MainForm.GetBiblioSummary,
                        summary_loader,
                        ref nRowIndex,
                        ref column_max_chars);
                            }
                            catch (Exception ex)
                            {
                                string strErrorText = "输出借阅历史时出现异常: " + ex.Message;
                                throw new Exception(strErrorText);
                            }
                        }

                        nRowIndex++;    // 读者之间的空行

                        nReaderIndex++;
                        return true;
                    },
                    out strError);
                if (nRet == -1)
                    return -1;

                {
                    if (stop != null)
                        stop.SetMessage("正在调整列宽度 ...");
                    Application.DoEvents();

                    //double char_width = GetAverageCharPixelWidth(list);

                    // 字符数太多的列不要做 width auto adjust
                    foreach (IXLColumn column in sheet.Columns())
                    {
                        int MAX_CHARS = 50;   // 60

                        int nIndex = column.FirstCell().Address.ColumnNumber - 1;
                        if (nIndex >= column_max_chars.Count)
                            break;
                        int nChars = column_max_chars[nIndex];

                        if (nIndex == 1)
                        {
                            column.Width = 10;
                            continue;
                        }

                        if (nIndex == 3)
                            MAX_CHARS = 50;
                        else
                            MAX_CHARS = 24;

                        if (nChars < MAX_CHARS)
                            column.AdjustToContents();
                        else
                            column.Width = Math.Min(MAX_CHARS, nChars);

                        //else
                        //    column.Width = (double)list.Columns[i].Width / char_width;  // Math.Min(MAX_CHARS, nChars);
                    }
                }

                this.ShowMessage("共导出读者记录 " + nReaderIndex + " 个", "green", true);
            }
            catch (Exception ex)
            {
                strError = "CreateDetailExcelFile() 出现异常: " + ExceptionUtil.GetExceptionText(ex);
                return -1;
            }
            finally
            {
                if (stop != null)
                    stop.SetMessage("");

                if (doc != null)
                {
                    doc.SaveAs(dlg.FileName);
                    doc.Dispose();
                }

                if (bLaunchExcel)
                {
                    try
                    {
                        System.Diagnostics.Process.Start(dlg.FileName);
                    }
                    catch
                    {

                    }
                }
            }

            // TODO: sheet 可以按照单位来区分。例如按照班级
            return 1;
        }
Exemple #3
0
        // 创建读者详情 Excel 文件。这是便于被外部调用的版本,只需要提供读者 XML 记录即可
        // return:
        //      -1  出错
        //      0   用户中断
        //      1   成功
        public static int CreateReaderDetailExcelFile(List<string> xmls,
            Delegate_GetBiblioSummary procGetBiblioSummary,
            Stop stop,
            bool bAdvanceXml,
            bool bLaunchExcel,
            out string strError)
        {
            strError = "";
            //int nRet = 0;

            ExportPatronExcelDialog dlg = new ExportPatronExcelDialog();
            MainForm.SetControlFont(dlg, Program.MainForm.Font, false);
            dlg.UiState = Program.MainForm.AppInfo.GetString(
        "ReaderSearchForm",
        "ExportPatronExcelDialog_uiState",
        "");

            Program.MainForm.AppInfo.LinkFormState(dlg, "ReaderSearchForm_ExportPatronExcelDialog_uiState_state");
            dlg.ShowDialog(Program.MainForm);

            Program.MainForm.AppInfo.SetString(
"ReaderSearchForm",
"ExportPatronExcelDialog_uiState",
dlg.UiState);

            if (dlg.DialogResult == System.Windows.Forms.DialogResult.Cancel)
            {
                strError = "放弃操作";
                return 0;
            }

            string strTimeRange = "";

            try
            {
                strTimeRange = GetTimeRange(dlg.ChargingHistoryDateRange);
            }
            catch (Exception ex)
            {
                strError = "日期范围字符串 '" + dlg.ChargingHistoryDateRange + "' 格式不合法: " + ex.Message;
                return -1;
            }

#if NO
            // 询问文件名
            SaveFileDialog dlg = new SaveFileDialog();

            dlg.Title = "请指定要输出的 Excel 文件名";
            dlg.CreatePrompt = false;
            dlg.OverwritePrompt = true;
            // dlg.FileName = this.ExportExcelFilename;
            // dlg.InitialDirectory = Environment.CurrentDirectory;
            dlg.Filter = "Excel 文件 (*.xlsx)|*.xlsx|All files (*.*)|*.*";

            dlg.RestoreDirectory = true;

            if (dlg.ShowDialog() != DialogResult.OK)
                return 0;
#endif

            XLWorkbook doc = null;

            try
            {
                doc = new XLWorkbook(XLEventTracking.Disabled);
                File.Delete(dlg.FileName);
            }
            catch (Exception ex)
            {
                strError = "ReaderSearchForm new XLWorkbook() {0BD1CB34-DF8A-4DDB-B884-8A9CF830D7C7} exception: " + ExceptionUtil.GetAutoText(ex);
                return -1;
            }

            IXLWorksheet sheet = null;
            sheet = doc.Worksheets.Add("表格");

            try
            {
                if (stop != null)
                    stop.SetProgressRange(0, xmls.Count);

                // 每个列的最大字符数
                List<int> column_max_chars = new List<int>();

                // TODO: 表的标题,创建时间

                int nRowIndex = 3;  // 空出前两行
                //int nColIndex = 1;

                int nReaderIndex = 0;
                foreach (string strXml in xmls)
                {
                    Application.DoEvents();	// 出让界面控制权

                    if (stop != null && stop.State != 0)
                        return 0;

                    if (string.IsNullOrEmpty(strXml) == true)
                        continue;

                    XmlDocument dom = new XmlDocument();
                    try
                    {
                        dom.LoadXml(strXml);
                    }
                    catch (Exception ex)
                    {
                        strError = "装载读者记录 XML 到 DOM 时发生错误: " + ex.Message;
                        return -1;
                    }

                    string strBarcode = DomUtil.GetElementText(dom.DocumentElement, "barcode");

                    // 
                    if (dlg.ExportReaderInfo)
                    {
                        OutputReaderInfo(sheet,
                dom,
                nReaderIndex,
                ref nRowIndex,
                ref column_max_chars);
                    }

                    // 输出在借册表格
                    if (dlg.ExportBorrowInfo)
                    {
                        OutputBorrows(sheet,
                dom,
                procGetBiblioSummary,
                bAdvanceXml,
                ref nRowIndex,
                ref column_max_chars);
                    }

                    // 输出违约金表格
                    if (dlg.ExportOverdueInfo)
                    {
                        OutputOverdues(sheet,
                dom,
                procGetBiblioSummary,
                ref nRowIndex,
                ref column_max_chars);
                    }

                    if (dlg.ExportChargingHistory)
                    {
                        LibraryChannel channel = Program.MainForm.GetChannel();
                        try
                        {
                            ChargingHistoryLoader history_loader = new ChargingHistoryLoader();
                            history_loader.Channel = channel;
                            history_loader.Stop = stop;
                            history_loader.PatronBarcode = strBarcode;
                            history_loader.TimeRange = strTimeRange;
                            history_loader.Actions = "return,lost";
                            history_loader.Order = "descending";

                            CacheableBiblioLoader summary_loader = new CacheableBiblioLoader();
                            summary_loader.Channel = channel;
                            summary_loader.Stop = stop;
                            summary_loader.Format = "summary";
                            summary_loader.GetBiblioInfoStyle = GetBiblioInfoStyle.None;
                            // summary_loader.RecPaths = biblio_recpaths;

                            // 输出借阅历史表格
                            // 可能会抛出异常,例如权限不够
                            OutputBorrowHistory(sheet,
                    dom,
                    history_loader,
                                // this.MainForm.GetBiblioSummary,
                    summary_loader,
                    ref nRowIndex,
                    ref column_max_chars);
                        }
                        catch (Exception ex)
                        {
                            strError = "输出借阅历史时出现异常: " + ex.Message;
                            return -1;
                        }
                        finally
                        {
                            Program.MainForm.ReturnChannel(channel);
                        }
                    }

                    nRowIndex++;    // 读者之间的空行

                    nReaderIndex++;
                    if (stop != null)
                        stop.SetProgressValue(nReaderIndex);
                }

                {
                    if (stop != null)
                        stop.SetMessage("正在调整列宽度 ...");
                    Application.DoEvents();

                    // 字符数太多的列不要做 width auto adjust
                    foreach (IXLColumn column in sheet.Columns())
                    {
                        int MAX_CHARS = 50;   // 60

                        int nIndex = column.FirstCell().Address.ColumnNumber - 1;
                        if (nIndex >= column_max_chars.Count)
                            break;
                        int nChars = column_max_chars[nIndex];

                        if (nIndex == 1)
                        {
                            column.Width = 10;
                            continue;
                        }

                        if (nIndex == 3)
                            MAX_CHARS = 50;
                        else
                            MAX_CHARS = 24;

                        if (nChars < MAX_CHARS)
                            column.AdjustToContents();
                        else
                            column.Width = Math.Min(MAX_CHARS, nChars);
                    }
                }
            }
            finally
            {
                if (doc != null)
                {
                    doc.SaveAs(dlg.FileName);
                    doc.Dispose();
                }

                if (bLaunchExcel)
                {
                    try
                    {
                        System.Diagnostics.Process.Start(dlg.FileName);
                    }
                    catch
                    {

                    }
                }

            }
            return 1;
        }