当前位置: 首页>>代码示例>>C#>>正文


C# Workbook.SaveCopyAs方法代码示例

本文整理汇总了C#中Microsoft.Office.Interop.Excel.Workbook.SaveCopyAs方法的典型用法代码示例。如果您正苦于以下问题:C# Workbook.SaveCopyAs方法的具体用法?C# Workbook.SaveCopyAs怎么用?C# Workbook.SaveCopyAs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.Office.Interop.Excel.Workbook的用法示例。


在下文中一共展示了Workbook.SaveCopyAs方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CompareLogs


//.........这里部分代码省略.........

                            //Läs in hela nuv. nya arket till en HT
                            Hashtable newRows = new Hashtable();
                            getExcelRows(newWorksheet, newRows);

                            int rows = 0;
                            int colums = 0;//newWorksheet.UsedRange.Columns.Count;
                            if (compareExcelRows(newWorksheet, oldBook[name] as Hashtable, newRows, ref rows, ref colums))
                            {
                                #region Sortera
                                //if (MainForm.StopGracefully)
                                //    break;

                                //Sortera på new
                                string column = GetStandardExcelColumnName(colums + 1);
                                Excel.Range range = newWorksheet.get_Range("A4", column + rows.ToString());//"IV"

                                if (name != "DatabaseInfo")// && name != "Info")
                                {
                                    range.Sort(range.Columns[colums + 1, Type.Missing], Excel.XlSortOrder.xlDescending//För att felsöka Excelprogrammering, använd macroEdit for VB i excel...
                                        //range.Columns[2,Type.Missing], Type.Missing, Excel.XlSortOrder.xlDescending
                                        , Type.Missing, Type.Missing, Excel.XlSortOrder.xlDescending
                                        , Type.Missing, Excel.XlSortOrder.xlDescending, Excel.XlYesNoGuess.xlNo
                                        , Type.Missing, Type.Missing, Excel.XlSortOrientation.xlSortColumns
                                        , Excel.XlSortMethod.xlPinYin, Excel.XlSortDataOption.xlSortNormal
                                        , Excel.XlSortDataOption.xlSortNormal, Excel.XlSortDataOption.xlSortNormal);

                                }

                                //Ta bort "new"-kolumnen
                                range = newWorksheet.get_Range(column + 1, column + rows.ToString());//"IV"
                                range.Delete(Type.Missing);//false//(object)false);//

                                if (name == "DatabaseInfo")//För old o diff överskriften...
                                {
                                    range = newWorksheet.get_Range("C:D", "C:D");
                                    range.EntireColumn.AutoFit();//autofittar hela columnen för all som loggas
                                }
                                //newWorksheet.set 
                                #endregion
                            }

                            //if (MainForm.StopGracefully)
                            //    break;

                        }
                        else
                        {
                            Console.WriteLine(name + " didn't exist in old Excel book.");
                        }
                        //oldBook.Add(name, oldRows);

                        //_compareProgress.SetTotal(++sheetsDone);

                    }


                    //Spara en ny fil
                    //newLog.FullName = newLog.FullName + "-Compared";
                    //newLog.Save();// .Save();
                    newLogFileName = _newLog.FullName.Replace(".xls", "") + "-Compared" + ".xls";
                    _newLog.SaveCopyAs(newLogFileName);//MainForm.LastLogPath);//newLog.FullName + "-Compared");
                    _newLog.Close(false, Type.Missing, Type.Missing);
                    //_book.Close(false, Type.Missing, Type.Missing);

                    _excelApp.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(_excelApp);
                    _excelApp = null;

                }
                catch (Exception e)
                {
                    //MessageBox.Show("Error in comparing old log with new log. Was the log opened in Excel during compare processing?\r\n\r\n(Sys err: " + e.Message + ").");
                    //throw e;
                    throw new Exception("Error in comparing old log with new log. Was the log opened in Excel during compare processing?\r\n\r\n(Sys err: " + e.Message + ").", e);
                }

                #endregion - read new

                System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;

            }
            catch (Exception e)
            {
                //MessageBox.Show("Error in comparing logs.\r\n\r\nSys err:\r\n\r\n" + e.Message, "Error, Exception!");
                throw e;
            }

            //MainForm.ShowTextProgress = false;//Visar inte ens texten längre
            //MainForm.ShowProgress = orgProgresSetting;//Håller på vad progress vad satt till innan

            //Stäng Excel
            if (_excelApp != null)
            {
                _excelApp.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(_excelApp);
                _excelApp = null;

            }
        }
开发者ID:perragradeen,项目名称:webbankbudgeter,代码行数:101,代码来源:ExcelLogRowComparer.cs

示例2: DataTableToExecl

        /// <summary>
        /// 将DataTable导出Excel
        /// </summary>
        /// <param name="dt">数据集</param>
        /// <param name="saveFilePath">保存路径</param>
        /// <param name="reportName">报表名称</param>
        /// <returns>是否成功</returns>
        public bool DataTableToExecl(DataTable dt, string saveFileName, string reportName)
        {
            //判断是否安装Excel
            bool fileSaved=false;
            if (status == -1)
                return fileSaved;
            //判断数据集是否为null
            if (dt == null)
            {
                returnMessage="无引出数据!";
                return false;
            }
            //判断保存路径是否有效
            if (!saveFileName.Contains(":"))
            {
                returnMessage="引出路径有误!请选择正确路径!";
                return false;
            }

            //创建excel对象
            workbooks=xlApp.Workbooks;
            workbook=workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
            worksheet=workbook.Worksheets[1];//取得sheet1
            worksheet.Cells.Font.Size=10;
            worksheet.Cells.NumberFormat="@";
            long totalCount=dt.Rows.Count;
            long rowRead=0;
            float percent=0;
            int rowIndex=0;

            //第一行为报表名称,如果为null则不保存该行    
            ++rowIndex;
            worksheet.Cells[rowIndex, 1]=reportName;
            range=worksheet.Cells[rowIndex, 1];
            range.Font.Bold=true;

            //写入字段(标题)
            ++rowIndex;
            for (int i=0; i < dt.Columns.Count; i++)
            {
                worksheet.Cells[rowIndex, i + 1]=dt.Columns[i].ColumnName;
                range=worksheet.Cells[rowIndex, i + 1];

                range.Font.Color=ColorTranslator.ToOle(Color.Blue);
                range.Interior.Color=dt.Columns[i].Caption == "表体" ? ColorTranslator.ToOle(Color.SkyBlue) : ColorTranslator.ToOle(Color.Yellow);
            }

            //写入数据
            ++rowIndex;
            for (int r=0; r < dt.Rows.Count; r++)
            {
                for (int i=0; i < dt.Columns.Count; i++)
                {
                    worksheet.Cells[r + rowIndex, i + 1]=dt.Rows[r][i].ToString();
                }
                rowRead++;
                percent=((float)(100 * rowRead)) / totalCount;
            }

            //画单元格边框
            range=worksheet.get_Range(worksheet.Cells[2, 1], worksheet.Cells[dt.Rows.Count + 2, dt.Columns.Count]);
            this.SetCellsBorderAround();

            //列宽自适应
            range.EntireColumn.AutoFit();

            //保存文件
            if (saveFileName != "")
            {
                try
                {
                    workbook.Saved=true;
                    workbook.SaveCopyAs(saveFileName);
                    fileSaved=true;
                }
                catch (Exception ex)
                {
                    fileSaved=false;
                    returnMessage="导出文件时出错,文件可能正被打开!\n" + ex.Message;
                }
            }
            else
            {
                fileSaved=false;
            }

            //释放Excel对应的对象(除xlApp,因为创建xlApp很花时间,所以等析构时才删除)
            //Dispose(false);
            Dispose();
            return fileSaved;
        }
开发者ID:elicec,项目名称:NFCGUI,代码行数:98,代码来源:ExcelIO.cs


注:本文中的Microsoft.Office.Interop.Excel.Workbook.SaveCopyAs方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。