コード例 #1
0
ファイル: AccountBookForm.cs プロジェクト: paopaofeng/dp2
        // 输出到WordXML文件
        private void button_print_outputWordXmlFile_Click(object sender, EventArgs e)
        {
            string strError = "";

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

            dlg.Title = "请指定要保存的WordML文件名";
            dlg.CreatePrompt = false;
            dlg.OverwritePrompt = true;
            dlg.FileName = this.ExportWordXmlFilename;
            // dlg.InitialDirectory = Environment.CurrentDirectory;
            dlg.Filter = "WordML文件 (*.xml)|*.xml|All files (*.*)|*.*";

            dlg.RestoreDirectory = true;

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

            this.ExportWordXmlFilename = dlg.FileName;

            OutputAcountBookTextFileDialog option_dialog = new OutputAcountBookTextFileDialog();
            MainForm.SetControlFont(option_dialog, this.Font, false);

            option_dialog.Text = "财产帐簿输出到WordML文件";
            option_dialog.MessageText = "请指定输出特性";
            option_dialog.Truncate = this.WordXmlTruncate;
            option_dialog.OutputStatisPart = this.WordXmlOutputStatisPart;
            option_dialog.StartPosition = FormStartPosition.CenterScreen;
            option_dialog.ShowDialog(this);

            if (option_dialog.DialogResult != DialogResult.OK)
                return;

            EnableControls(false);

            try
            {

                this.WordXmlTruncate = option_dialog.Truncate;
                this.WordXmlOutputStatisPart = option_dialog.OutputStatisPart;


                XmlTextWriter writer = null;

                try
                {
                    writer = new XmlTextWriter(this.ExportWordXmlFilename, Encoding.UTF8);
                }
                catch (Exception ex)
                {
                    strError = "创建文件 '" + ExportWordXmlFilename + "' 时发生错误: " + ex.Message;
                    goto ERROR1;
                }

                writer.Formatting = Formatting.Indented;
                writer.Indentation = 4;

                try
                {
                    Cursor oldCursor = this.Cursor;
                    this.Cursor = Cursors.WaitCursor;

                    int nCount = 0;
                    List<ListViewItem> items = new List<ListViewItem>();
                    for (int i = 0; i < this.listView_in.Items.Count; i++)
                    {
                        ListViewItem item = this.listView_in.Items[i];

                        items.Add(item);
                        nCount++;
                    }


                    // 输出到Word XML文件
                    int nRet = OutputToWordXmlFile(
                        items,
                        writer,
                        out strError);
                    if (nRet == -1)
                        goto ERROR1;

                    this.Cursor = oldCursor;

                    this.MainForm.StatusBarMessage = "财产帐簿内容 " + nCount.ToString() + "个 已成功创建到文件 " + this.ExportWordXmlFilename;
                }
                finally
                {
                    if (writer != null)
                    {
                        writer.Close();
                        writer = null;
                    }
                }
            }
            finally
            {
                EnableControls(true);
            }

            return;
        ERROR1:
            MessageBox.Show(this, strError);
        }
コード例 #2
0
ファイル: AccountBookForm.cs プロジェクト: paopaofeng/dp2
        private void button_print_outputExcelFile_Click(object sender, EventArgs e)
        {
            string strError = "";

            if (this.listView_in.Items.Count == 0)
            {
                strError = "没有可输出的行";
                goto ERROR1;
            }

            XLWorkbook doc = null;

            // 询问文件名
            // 询问文件名
            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;

            this.ExportExcelFilename = dlg.FileName;

#if NO
            try
            {
                doc = ExcelDocument.Create(this.ExportExcelFilename);
            }
            catch (Exception ex)
            {
                strError = ex.Message;
                goto ERROR1;
            }

            doc.Stylesheet = GenerateStyleSheet();
#endif
            try
            {
                doc = new XLWorkbook(XLEventTracking.Disabled);
                File.Delete(this.ExportExcelFilename);
            }
            catch (Exception ex)
            {
                strError = ex.Message;
                goto ERROR1;
            }
            OutputAcountBookTextFileDialog option_dialog = new OutputAcountBookTextFileDialog();
            MainForm.SetControlFont(option_dialog, this.Font, false);

            option_dialog.Truncate = this.TextTruncate;
            option_dialog.OutputStatisPart = this.TextOutputStatisPart;
            option_dialog.StartPosition = FormStartPosition.CenterScreen;
            option_dialog.ShowDialog(this);

            if (option_dialog.DialogResult != DialogResult.OK)
                return;

            this.TextTruncate = option_dialog.Truncate;
            this.TextOutputStatisPart = option_dialog.OutputStatisPart;

            EnableControls(false);

            stop.OnStop += new StopEventHandler(this.DoStop);
            stop.Initial("正在构造财产帐 ...");
            stop.BeginLoop();

            try
            {
                int nCount = 0;
                List<ListViewItem> items = new List<ListViewItem>();
                for (int i = 0; i < this.listView_in.Items.Count; i++)
                {
                    ListViewItem item = this.listView_in.Items[i];

                    items.Add(item);
                    nCount++;
                }

                // 输出到文本文件
                int nRet = OutputToTextFile(
                    items,
                    null,
                    ref doc,
                    out strError);
                if (nRet == -1)
                    goto ERROR1;

                if (doc != null)
                {
                    // Close the document.
                    // doc.Close();
                    doc.SaveAs(this.ExportExcelFilename);
                }

                this.MainForm.StatusBarMessage = "财产帐簿内容 " + nCount.ToString() + "个 已成功输出到文件 " + this.ExportExcelFilename;
            }
            finally
            {
                stop.EndLoop();
                stop.OnStop -= new StopEventHandler(this.DoStop);
                stop.Initial("");
                stop.HideProgress();

                EnableControls(true);
            }

            return;
        ERROR1:
            MessageBox.Show(this, strError);
        }
コード例 #3
0
ファイル: AccountBookForm.cs プロジェクト: paopaofeng/dp2
        // 输出到文本文件
        private void button_print_outputTextFile_Click(object sender, EventArgs e)
        {
            string strError = "";

            if (this.listView_in.Items.Count == 0)
            {
                strError = "没有可输出的行";
                goto ERROR1;
            }

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

            dlg.Title = "请指定要保存的文本文件名";
            dlg.CreatePrompt = false;
            dlg.OverwritePrompt = false;
            dlg.FileName = this.ExportTextFilename;
            // dlg.InitialDirectory = Environment.CurrentDirectory;
            dlg.Filter = "文本文件 (*.txt)|*.txt|All files (*.*)|*.*";

            dlg.RestoreDirectory = true;

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

            this.ExportTextFilename = dlg.FileName;

            OutputAcountBookTextFileDialog option_dialog = new OutputAcountBookTextFileDialog();
            MainForm.SetControlFont(option_dialog, this.Font, false);

            option_dialog.Truncate = this.TextTruncate;
            option_dialog.OutputStatisPart = this.TextOutputStatisPart;
            option_dialog.StartPosition = FormStartPosition.CenterScreen;
            option_dialog.ShowDialog(this);

            if (option_dialog.DialogResult != DialogResult.OK)
                return;

            this.TextTruncate = option_dialog.Truncate;
            this.TextOutputStatisPart = option_dialog.OutputStatisPart;

            bool bAppend = true;

            if (File.Exists(this.ExportTextFilename) == true)
            {
                DialogResult result = MessageBox.Show(this,
                    "文本文件 '" + this.ExportTextFilename + "' 已经存在。\r\n\r\n本次输出内容是否要追加到该文件尾部? (Yes 追加;No 覆盖;Cancel 放弃输出)",
                    "AccountBookForm",
                    MessageBoxButtons.YesNoCancel,
                    MessageBoxIcon.Question,
                    MessageBoxDefaultButton.Button1);
                if (result == DialogResult.Cancel)
                    return;
                if (result == DialogResult.No)
                    bAppend = false;
            }

            EnableControls(false);
            stop.OnStop += new StopEventHandler(this.DoStop);
            stop.Initial("正在构造财产帐 ...");
            stop.BeginLoop();

            // 创建文件
            StreamWriter sw = new StreamWriter(this.ExportTextFilename,
                bAppend,	// append
                System.Text.Encoding.UTF8);
            try
            {
                Cursor oldCursor = this.Cursor;
                this.Cursor = Cursors.WaitCursor;

                int nCount = 0;
                List<ListViewItem> items = new List<ListViewItem>();
                for (int i = 0; i < this.listView_in.Items.Count; i++)
                {
                    ListViewItem item = this.listView_in.Items[i];

                    items.Add(item);
                    nCount++;
                }

                XLWorkbook doc = null;

                // 输出到文本文件
                int nRet = OutputToTextFile(
                    items,
                    sw,
                    ref doc,
                    out strError);
                if (nRet == -1)
                    goto ERROR1;

                this.Cursor = oldCursor;

                string strExportStyle = "创建";
                if (bAppend == true)
                    strExportStyle = "追加";

                this.MainForm.StatusBarMessage = "财产帐簿内容 " + nCount.ToString() + "个 已成功" + strExportStyle + "到文件 " + this.ExportTextFilename;

            }
            finally
            {
                if (sw != null)
                    sw.Close();

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

                EnableControls(true);
            }

            return;
            ERROR1:
                MessageBox.Show(this, strError);
        }