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


C# Worksheet.Copy方法代码示例

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


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

示例1: CreateExcelItem

        private unsafe void CreateExcelItem()
        {
            // Создаем документ с 16 страницами
            excelapp = new Excel.Application();
            //excelapp.Visible=true;

            excelapp.SheetsInNewWorkbook=1;
            Excel.Workbook excelappworkbook = excelapp.Workbooks.Add(Type.Missing);

            String[] SheetsName = new String[16] { "Sword", "Axe", "MaceScepter", "Spear", "BowCrossbow", "Staff", "Shield", "Helm", "Armor", "Pants", "Gloves", "Boots", "Accessories", "Misc1", "Misc2", "Scrolls" };

            excelsheets = excelappworkbook.Worksheets;

            // определяем имена страницам и переходим на страницу
            excelworksheet = (Excel.Worksheet)excelsheets.get_Item(0 + 1);
            excelworksheet.Name = SheetsName[0];
            excelworksheet.Activate();
            excelworksheet.Application.ActiveWindow.SplitColumn = 3;
            excelworksheet.Application.ActiveWindow.SplitRow = 2;
            excelworksheet.Application.ActiveWindow.FreezePanes = true;

            // заполнение Index (0.1.2.3...)
            excelcells = excelworksheet.get_Range("B3", Type.Missing);
            excelcells.Value2 = 0;
            excelcells = excelworksheet.get_Range("B4", Type.Missing);
            excelcells.Value2 = 1;
            excelcells = excelworksheet.get_Range("B3", "B4");
            Excel.Range dest = excelworksheet.get_Range("B3", "B514");
            excelcells.AutoFill(dest, Excel.XlAutoFillType.xlFillDefault);

            // сворачиваем для увеличения скорости
            excelworksheet.Application.WindowState = Excel.XlWindowState.xlMinimized;
            excelworksheet.Application.Visible = false;

            // оцентровываем первую строку
            excelcells = (Excel.Range)excelworksheet.Rows["1", Type.Missing];
            excelcells.HorizontalAlignment = Excel.Constants.xlCenter;

            // зажирняем и оцентровываем вторую строку
            excelcells = (Excel.Range)excelworksheet.Rows["2", Type.Missing];
            excelcells.Font.Bold = true;
            excelcells.HorizontalAlignment = Excel.Constants.xlCenter;

            // устанавливаем размер колонок
            excelcells = (Excel.Range)excelworksheet.Columns["A", Type.Missing];
            excelcells.ColumnWidth = 5;
            excelcells = (Excel.Range)excelworksheet.Columns["B", Type.Missing];
            excelcells.ColumnWidth = 5;
            excelcells = (Excel.Range)excelworksheet.Columns["C", Type.Missing];
            excelcells.ColumnWidth = 30;
            for (int j = 0; j < MyItemColumns.Length; j++)
            {
                excelcells = (Excel.Range)excelworksheet.Columns[ColumnTempName[j + 3], Type.Missing];
                excelcells.ColumnWidth = MyItemColumns[j].Width;
            }

            // заполняем первую строку границами как называется не помню
            excelcells = excelworksheet.get_Range("C1", Type.Missing);
            excelcells.Value2 = "Char[30]";
            excelcells.Activate();
            for (int j = 0; j < MyItemColumns.Length; j++)
            {
                excelcells = excelapp.ActiveCell.get_Offset(0, 1);
                excelcells.Value2 = MyItemColumns[j].ColSize;
                excelcells.Activate();
            }

            // заполняем вторую строку названиями
            excelcells = excelworksheet.get_Range("A2", Type.Missing);
            excelcells.Value2 = "Type";
            excelcells = excelworksheet.get_Range("B2", Type.Missing);
            excelcells.Value2 = "Index";
            excelcells = excelworksheet.get_Range("C2", Type.Missing);
            excelcells.Value2 = "Item Name";
            excelcells.Activate();
            for (int j = 0; j < MyItemColumns.Length; j++)
            {
                excelcells = excelapp.ActiveCell.get_Offset(0, 1);
                excelcells.Value2 = MyItemColumns[j].Name;
                excelcells.Activate();
            }

            // обнуляем все ячейки кроме названия
            excelcells = excelworksheet.get_Range("D3", "AR514");
            excelcells.Value2 = 0;

            // number format 12 232 232 для zen
            excelcells = excelworksheet.get_Range("AB3", "AB514");
            excelcells.NumberFormat = "# ##0";

            // копируем листы
            for (int i = 0; i < 15; i++)
            {
                excelworksheet = (Excel.Worksheet)excelsheets.get_Item(i + 1);
                excelworksheet.Copy(Type.Missing, excelworksheet);
                excelworksheet = (Excel.Worksheet)excelsheets.get_Item(i + 2);
                excelworksheet.Name = SheetsName[i + 1];
            }

            // заполняем ячейки
//.........这里部分代码省略.........
开发者ID:samik3k,项目名称:BMDtoExcel,代码行数:101,代码来源:Form1.cs

示例2: DataTableToExcel

        /// <summary>
        /// 将DataTable数据写入Excel文件(自动分页)
        /// </summary>
        /// <param name="dt">DataTable</param>
        /// <param name="rows">每个WorkSheet写入多少行数据</param>
        /// <param name="top">表格数据起始行索引</param>
        /// <param name="left">表格数据起始列索引</param>
        public void DataTableToExcel(DataTable dt, int rows, int top, int left)
        {
            int rowCount = dt.Rows.Count;  //DataTable行数
            int colCount = dt.Columns.Count; //DataTable列数
            sheetCount = this.GetSheetCount(rowCount, rows); //WorkSheet个数
            //   StringBuilder sb;

            //复制sheetCount-1个WorkSheet对象
            for (int i = 1; i < sheetCount; i++)
            {
                workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
                workSheet.Copy(missing, workBook.Worksheets[i]);
            }

            for (int i = 1; i <= sheetCount; i++)
            {
                int startRow = (i - 1) * rows;  //记录起始行索引
                int endRow = i * rows;   //记录结束行索引

                //若是最后一个WorkSheet,那么记录结束行索引为源DataTable行数
                if (i == sheetCount)
                    endRow = rowCount;

                //获取要写入数据的WorkSheet对象,并重命名
                workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
                workSheet.Name = sheetPrefixName + "-" + i.ToString();

                //将dt中的数据写入WorkSheet
                //    for(int j=0;j<endRow-startRow;j++)
                //    {
                //     for(int k=0;k<colCount;k++)
                //     {
                //      workSheet.Cells[top + j,left + k] = dt.Rows[startRow + j][k].ToString();
                //     }
                //    }

                //利用二维数组批量写入
                int row = endRow - startRow;
                string[,] ss = new string[row, colCount];

                for (int j = 0; j < row; j++)
                {
                    for (int k = 0; k < colCount; k++)
                    {
                        ss[j, k] = dt.Rows[startRow + j][k].ToString();
                    }
                }

                range = (Excel.Range)workSheet.Cells[top, left];
                range = range.get_Resize(row, colCount);
                range.Value = ss;

                #region 利用Windwo粘贴板批量拷贝数据(在Web下面行不通)
                /*sb = new StringBuilder();

            for(int j=0;j<endRow-startRow;j++)
            {
             for(int k=0;k<colCount;k++)
             {
              sb.Append( dt.Rows[startRow + j][k].ToString() );
              sb.Append("\t");
             }

             sb.Append("\n");
            }

            System.Windows.Forms.Clipboard.SetDataObject(sb.ToString());

            range = (Excel.Range)workSheet.Cells[top,left];
            workSheet.Paste(range,false);*/
                #endregion

            }
        }
开发者ID:ramic,项目名称:ramic,代码行数:81,代码来源:ExcelBase.cs

示例3: ArrayToExcel

        /// <summary>
        /// 将二维数组数据写入Excel文件(自动分页,并指定要合并的列索引)
        /// </summary>
        /// <param name="arr">二维数组</param>
        /// <param name="rows">每个WorkSheet写入多少行数据</param>
        /// <param name="top">行索引</param>
        /// <param name="left">列索引</param>
        /// <param name="mergeColumnIndex">数组的二维索引,相当于DataTable的列索引,索引从0开始</param>
        public void ArrayToExcel(string[,] arr, int rows, int top, int left, int mergeColumnIndex)
        {
            int rowCount = arr.GetLength(0);  //二维数组行数(一维长度)
            int colCount = arr.GetLength(1); //二维数据列数(二维长度)
            sheetCount = this.GetSheetCount(rowCount, rows); //WorkSheet个数

            //复制sheetCount-1个WorkSheet对象
            for (int i = 1; i < sheetCount; i++)
            {
                workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
                workSheet.Copy(missing, workBook.Worksheets[i]);
            }

            //将二维数组数据写入Excel
            for (int i = sheetCount; i >= 1; i--)
            {
                int startRow = (i - 1) * rows;  //记录起始行索引
                int endRow = i * rows;   //记录结束行索引

                //若是最后一个WorkSheet,那么记录结束行索引为源DataTable行数
                if (i == sheetCount)
                    endRow = rowCount;

                //获取要写入数据的WorkSheet对象,并重命名
                workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
                workSheet.Name = sheetPrefixName + "-" + i.ToString();

                //将二维数组中的数据写入WorkSheet
                for (int j = 0; j < endRow - startRow; j++)
                {
                    for (int k = 0; k < colCount; k++)
                    {
                        workSheet.Cells[top + j, left + k] = arr[startRow + j, k];
                    }
                }

                //利用二维数组批量写入
                int row = endRow - startRow;
                string[,] ss = new string[row, colCount];

                for (int j = 0; j < row; j++)
                {
                    for (int k = 0; k < colCount; k++)
                    {
                        ss[j, k] = arr[startRow + j, k];
                    }
                }

                range = (Excel.Range)workSheet.Cells[top, left];
                range = range.get_Resize(row, colCount);
                range.Value = ss;

                //合并相同行
                this.MergeRows(workSheet, left + mergeColumnIndex, top, rows);
            }
        }
开发者ID:ramic,项目名称:ramic,代码行数:64,代码来源:ExcelBase.cs


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