Example #1
0
        // 输出到文本文件
        int OutputToTextFile(
            List<ListViewItem> items,
            StreamWriter sw,
            ref XLWorkbook doc,
            out string strError)
        {
            strError = "";
            int nRet = 0;

            Hashtable macro_table = new Hashtable();

            string strNamePath = "accountbook_printoption_text";

            // 获得打印参数
            PrintOption option = new AccountBookPrintOption(this.MainForm.DataDir,
                this.comboBox_load_type.Text);
            option.LoadData(this.MainForm.AppInfo,
                strNamePath);

            // 检查当前排序状态和包含种价格列之间是否存在矛盾
            if (bHasBiblioPriceColumn(option) == true)
            {

                if (this.SortColumns_in.Count != 0
                    && this.SortColumns_in[0].No == COLUMN_BIBLIORECPATH)
                {
                }
                else
                {
                    MessageBox.Show(this, "由于当前打印用到了 “种价格”列,为保证打印结果的准确,程序自动按 ‘种记录路径’ 列对全部列表事项进行一次自动排序。\r\n\r\n为避免这里的自动排序,可在打印前用鼠标左键点栏标题进行符合自己意愿的排序,只要最后一次点的是‘种记录路径’栏标题即可。");
                    ForceSortColumnsIn(COLUMN_BIBLIORECPATH);
                }
            }

            // 2009/7/24 changed
            if (this.SourceStyle == "batchno")
            {
                macro_table["%batchno%"] = HttpUtility.HtmlEncode(this.BatchNo); // 批次号
                macro_table["%location%"] = HttpUtility.HtmlEncode(this.LocationString); // 馆藏地点 用HtmlEncode()的原因是要防止里面出现的“<不指定>”字样
            }
            else
            {
                macro_table["%batchno%"] = "";
                macro_table["%location%"] = "";
            }

            // macro_table["%pagecount%"] = nPageCount.ToString();
            // macro_table["%linesperpage%"] = option.LinesPerPage.ToString();
            macro_table["%date%"] = DateTime.Now.ToLongDateString();

            // 2009/7/24 changed
            if (this.SourceStyle == "barcodefile")
            {
                macro_table["%barcodefilepath%"] = this.BarcodeFilePath;
                macro_table["%barcodefilename%"] = Path.GetFileName(this.BarcodeFilePath);
            }
            else
            {
                macro_table["%barcodefilepath%"] = "";
                macro_table["%barcodefilename%"] = "";
            }

            // 2009/7/30 
            if (this.SourceStyle == "recpathfile")
            {
                macro_table["%recpathfilepath%"] = this.RecPathFilePath;
                macro_table["%recpathfilename%"] = Path.GetFileName(this.RecPathFilePath);
            }
            else
            {
                Debug.Assert(this.SourceStyle == "batchno" || this.SourceStyle == "barcodefile", "");

                macro_table["%recpathfilepath%"] = "";
                macro_table["%recpathfilename%"] = "";
            }

            macro_table["%sourcedescription%"] = this.SourceDescription;

            IXLWorksheet sheet = null;

            // 输出统计信息页
            if (this.TextOutputStatisPart == true)
            {
                if (doc != null)
                {
                    sheet = doc.Worksheets.Add("统计页");
                    sheet.Style.Font.FontName = this.Font.Name;
                }

                int nItemCount = items.Count;
                int nBiblioCount = GetBiblioCount(items);
                string strTotalPrice = GetTotalPrice(items);

                macro_table["%itemcount%"] = nItemCount.ToString();
                macro_table["%bibliocount%"] = nBiblioCount.ToString();
                macro_table["%totalprice%"] = strTotalPrice;

                macro_table["%pageno%"] = "1";

                // 2008/11/23 
                macro_table["%datadir%"] = this.MainForm.DataDir;   // 便于引用datadir下templates目录内的某些文件
                ////macro_table["%libraryserverdir%"] = this.MainForm.LibraryServerDir;  // 便于引用服务器端的CSS文件

                string strTemplateFilePath = option.GetTemplatePageFilePath("统计页");
                if (String.IsNullOrEmpty(strTemplateFilePath) == false)
                {
                    /*
                     * TODO:修改为纯文本方式
<html>
<head>
	<LINK href='%libraryserverdir%/accountbook.css' type='text/css' rel='stylesheet'>
</head>
<body>
	<div class='pageheader'>%date% 财产帐簿 -- %sourcedescription% -- (共 %pagecount% 页)</div>
	<div class='tabletitle'>%date% 财产帐簿 -- %sourcedescription%</div>
	<div class='itemcount'>册数: %itemcount%</div>
	<div class='bibliocount'>种数: %bibliocount%</div>
	<div class='totalprice'>总价: %totalprice%</div>
	<div class='sepline'><hr/></div>
	<div class='batchno'>批次号: %batchno%</div>
	<div class='location'>馆藏地点: %location%</div>
	<div class='location'>条码号文件: %barcodefilepath%</div>
	<div class='location'>记录路径文件: %recpathfilepath%</div>
	<div class='pagefooter'>%pageno%/%pagecount%</div>
</body>
</html>
                     * * */

                    // 根据模板打印
                    string strContent = "";
                    // 能自动识别文件内容的编码方式的读入文本文件内容模块
                    // return:
                    //      -1  出错
                    //      0   文件不存在
                    //      1   文件存在
                    nRet = Global.ReadTextFileContent(strTemplateFilePath,
                        out strContent,
                        out strError);
                    if (nRet == -1)
                        return -1;

                    string strResult = StringUtil.MacroString(macro_table,
                        strContent);

                    if (sw != null)
                        sw.WriteLine(strResult);

                    // TODO: string --> excel page
                }
                else
                {
                    // 缺省的固定内容打印

                    // 内容行
                    if (sw != null)
                    {
                        sw.WriteLine("册数\t" + nItemCount.ToString());
                        sw.WriteLine("种数\t" + nBiblioCount.ToString());
                        sw.WriteLine("总价\t" + strTotalPrice);

                        sw.WriteLine("----------");


                        if (this.SourceStyle == "batchno")
                        {
                            // 2008/11/22 
                            if (String.IsNullOrEmpty(this.BatchNo) == false)
                            {
                                sw.WriteLine("批次号\t" + this.BatchNo);
                            }
                            if (String.IsNullOrEmpty(this.LocationString) == false
                                && this.LocationString != "<不指定>")
                            {
                                sw.WriteLine("馆藏地点\t" + this.LocationString);
                            }
                        }

                        if (this.SourceStyle == "barcodefile")
                        {
                            if (String.IsNullOrEmpty(this.BarcodeFilePath) == false)
                            {
                                sw.WriteLine("条码号文件\t" + this.BarcodeFilePath);
                            }
                        }

                        // 2009/7/30 
                        if (this.SourceStyle == "recpathfile")
                        {
                            if (String.IsNullOrEmpty(this.RecPathFilePath) == false)
                            {
                                sw.WriteLine("记录路径文件\t" + this.RecPathFilePath);
                            }
                        }


                        sw.WriteLine("----------");
                        sw.WriteLine("");
                    }

                    if (doc != null)
                    {
#if NO
                        int nLineIndex = 2;

                        doc.WriteExcelLine(
    nLineIndex++,
    "册数",
    nItemCount.ToString());

                        doc.WriteExcelLine(
    nLineIndex++,
    "种数",
    nBiblioCount.ToString());

                        doc.WriteExcelLine(
nLineIndex++,
"总价",
strTotalPrice);
#endif
                        
                        int nLineIndex = 2;

                        WriteValuePair(sheet,
    nLineIndex++,
    "册数",
    nItemCount.ToString());

                        WriteValuePair(sheet,
    nLineIndex++,
    "种数",
    nBiblioCount.ToString());

                        WriteValuePair(sheet,
nLineIndex++,
"总价",
strTotalPrice);           
                    }

                }

            }

            string strMarcFilterFilePath = option.GetTemplatePageFilePath("MARC过滤器");
            if (String.IsNullOrEmpty(strMarcFilterFilePath) == false)
            {
                nRet = PrepareMarcFilter(strMarcFilterFilePath, out strError);
                if (nRet == -1)
                    return -1;
            }

            if (doc != null)
            {
                sheet = doc.Worksheets.Add("财产帐");
                sheet.Style.Font.FontName = this.Font.Name;

#if NO
                Columns columns = new Columns();
                DocumentFormat.OpenXml.Spreadsheet.Column column = new DocumentFormat.OpenXml.Spreadsheet.Column();
                column.Min = 4;
                column.Max = 4;
                column.Width = 40;
                column.CustomWidth = true;
                columns.Append(column);

                doc.WorkSheet.InsertAt(columns, 0);
#endif
#if NO
                List<int> widths = new List<int>(new int [] {4,4,4,40});
                SetColumnWidth(doc, widths);
#endif
            }

            // 构造表格标题和标题行
            BuildTextPageTop(option,
                macro_table,
                sw,
                sheet);

            stop.SetProgressValue(0);
            stop.SetProgressRange(0, items.Count);

            // 表格行循环
            for (int i = 0; i < items.Count; i++)
            {
                Application.DoEvents();	// 出让界面控制权

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

                BuildTextTableLine(option,
                    items,
                    i,
                    sw,
                    // ref doc,
                    sheet,
                    this.TextTruncate);

                stop.SetProgressValue(i + 1);
            }

            return 0;
        }