예제 #1
0
        public static void ExportLinesToExcel(
            Form form,
            ListView list)
        {
            // 询问文件名
            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;
            }

            ExcelDocument doc = null;
            // Sheet sheet = null;
            int _lineIndex = 0;

            doc = ExcelDocument.Create(dlg.FileName);

            doc.NewSheet("Sheet1");

            int nColIndex = 0;

            foreach (ColumnHeader header in list.Columns)
            {
                doc.WriteExcelCell(
                    _lineIndex,
                    nColIndex++,
                    header.Text,
                    true);
            }
            _lineIndex++;

            Cursor oldCursor = form.Cursor;

            form.Cursor = Cursors.WaitCursor;

            for (int i = 0; i < list.SelectedIndices.Count; i++)
            {
                int index = list.SelectedIndices[i];

                ListViewItem item = list.Items[index];

                List <CellData> cells = new List <CellData>();

                nColIndex = 0;
                foreach (ListViewItem.ListViewSubItem subitem in item.SubItems)
                {
                    cells.Add(
                        new CellData(
                            nColIndex++,
                            subitem.Text,
                            true,
                            0)
                        );
                }

                doc.WriteExcelLine(
                    _lineIndex,
                    cells,
                    WriteExcelLineStyle.None);

                _lineIndex++;
            }

            doc.SaveWorksheet();
            if (doc != null)
            {
                doc.Close();
                doc = null;
            }

            form.Cursor = oldCursor;
        }
예제 #2
0
        /// <summary>
        /// Execute the Excel Result.
        /// </summary>
        /// <param name="context">Controller context.</param>
        public override void ExecuteResult(ControllerContext context)
        {
            MemoryStream stream = ExcelDocument.Create(this.excelFileName, this.excelWorkSheetName, this.rowDataTable, this.headerData, this.rowPointers);

            WriteStream(stream, this.excelFileName);
        }