winform 向excel中追加sheet

/// <summary>
        /// 通过DataTable向一个现有的excel表中增加一个sheet
        /// </summary>
        /// <param name="dt">带有数据集的DataTable</param>
        /// <param name="toFileName">现有excel的路径以及名称</param>
        /// <param name="strSheetName">需要添加的Sheet的名字</param>
        private void doExport(DataTable dt, string toFileName, string strSheetName)
        {
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();  //Execl的操作类
            //读取保存目标的对象
            Microsoft.Office.Interop.Excel.Workbook bookDest = excel.Workbooks._Open(toFileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            //打开要导出到的Execl文件的工作薄。--ps:关于Missing类在这里的作用,我也不知道...囧
            Microsoft.Office.Interop.Excel.Worksheet sheetDest = bookDest.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value) as Microsoft.Office.Interop.Excel.Worksheet;
            //给工作薄添加一个Sheet
            sheetDest.Name = strSheetName;//自己定义名字O(∩_∩)O哈哈~
            int rowIndex = 1;
            int colIndex = 0;
            excel.Application.Workbooks.Add(true);//这句不写不知道会不会报错
            Microsoft.Office.Interop.Excel.Range range = null;

            foreach (DataColumn col in dt.Columns)
            {

                colIndex++;
                sheetDest.Cells[1, colIndex] = col.ColumnName;//Execl中的第一列,把DataTable的列名先导进去
                sheetDest.get_Range(sheetDest.Cells[1, 1], sheetDest.Cells[1, dt.Columns.Count]).HorizontalAlignment = Microsoft.Office.Core.XlHAlign.xlHAlignCenterAcrossSelection;
                range = (Microsoft.Office.Interop.Excel.Range)sheetDest.Cells[1, colIndex];
                range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin,
                               Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null);
                range.Font.Bold = true; //粗体
            }

            #region
            //导入数据行
            //foreach (DataRow row in dt.Rows)
            //{
            //    rowIndex++;
            //    colIndex = 0;
            //    foreach (DataColumn col in dt.Columns)
            //    {
            //        colIndex++;
            //        sheetDest.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
            //    }
            //}
            #endregion
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow row = dt.Rows[i];
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    DataColumn col = dt.Columns[j];
                    sheetDest.Cells[i + 2, j + 1] = row[col.ColumnName].ToString();
                    range = (Microsoft.Office.Interop.Excel.Range)sheetDest.Cells[i + 2, j + 1];
                    range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin,
                                   Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null);
                }
            }
            sheetDest.Columns.EntireColumn.AutoFit();//列宽自适应
            if (toFileName.ToLower().Contains("wish"))
            {
                sheetDest.get_Range(sheetDest.Cells[1, 1], sheetDest.Cells[dt.Rows.Count + 1, dt.Columns.Count - 5]).HorizontalAlignment = Microsoft.Office.Core.XlHAlign.xlHAlignCenterAcrossSelection;
            }
            else
            {
                sheetDest.get_Range(sheetDest.Cells[1, 1], sheetDest.Cells[dt.Rows.Count + 1, dt.Columns.Count - 4]).HorizontalAlignment = Microsoft.Office.Core.XlHAlign.xlHAlignCenterAcrossSelection;
            }
            bookDest.Saved = true; bookDest.Save();
            excel.Quit();
            excel = null;
            GC.Collect();//垃圾回收
        }
时间: 2024-11-12 17:04:24

winform 向excel中追加sheet的相关文章

java jxl 向Excel中追加数据而不覆盖原来数据的例子

向先原来就有数据的Excel写数据是不会覆盖原有的数据,只是在追加数据. public class Excel { public Excel() { } public void CreateWorkbook(File file, double[] a) { try { if (!file.exists()) { //判断文件是否已存在,如果没有存在则创建新文件 jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File("r

向Excel中追加数据

读取Excel模版插入数据并生成一个新的文件保存 本文中接受03的excel操作(跟07一样不过 其中的  HSSF  换成 XSSF) FileInputStream fileInputStream = new FileInputStream(templeExcelPath); HSSFWorkbook xssfWorkbook = new HSSFWorkbook(fileInputStream); HSSFSheet xssfSheet = xssfWorkbook.getSheetAt(

计算Excel中的Sheet个数

$strpath="d:\ee.xlsx"$excel=new-object -comobject excel.application$WorkBook = $excel.Workbooks.Open($strpath)($WorkBook.worksheets).count$excel.Quit()$WorkSheet =$null$WorkBook = $null$excel = $null[GC]::Collect()

POI向Excel中写入数据及追加数据

import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.*; import java.util.ArrayList; import java.

python实现自动从mysql数据库取指定数据记录到excel中-新建、追加

xlsxwriter,openpyxl,pandas 模块都可以实现往excel中写入数据,但是为了更简单方便的实现我的需求,选择将三种结合使用. #!/usr/bin/env python3 # -*-coding: utf-8 -*- # @Time:2019/12/26 16:55 # @Author: WSN import pandas as pd import pymysql, openpyxl, os, xlsxwriter # 设定excel文件名称 version = 'V1.4

WinForm 读取Excel 数据显示到窗体中

最近教学中,需要用到WinForm 读取Excel数据,于是就做了一个简单的,废话不多说,直接codding... 1 //读取Excel的帮助类 2 class SqExcellHelper 3 { 4 public static DataTable GetData(string tablename) 5 { 6 DataTable dtEmp = new DataTable(tablename); 7 OleDbConnection con = new OleDbConnection();

C#获取Excel中所有的Sheet名称《转》以备忘

Excel.Application myExcel = new Excel.Application();object missing = System.Reflection.Missing.Value;myExcel.Application.Workbooks.Open(this.txtFile.Text, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missi

VBA excel中批量创建超链接代码(连接当前文档中的sheet)

excel中批量创建超链接代码(连接当前文档中的sheet),在sheet1中B列中要创建一系列的超链接,链接的内容是本文档中的其他sheet,如下图,在sheet1下创建宏,代码如下. Sub 宏1() Dim temp, temp2 Dim i, j j = 1 For i = 5 To 74 temp = "'G" & j & "'!A1" temp2 = "G" & j Range("B" &a

EXCEL中如何获得工作表(sheet)的名称

1.  EXCEL中获得工作表(sheet)的名称: RIGHT(CELL("filename"),LEN(CELL("filename"))-FIND("]",CELL("filename"))) =MID(CELL("filename",$A$1),FIND("]",CELL("filename",$A$1))+1,LEN(CELL("filename&