winform 向excel中追加sheet
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了winform 向excel中追加sheet,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3160字,纯文字阅读大概需要5分钟。
内容图文
![winform 向excel中追加sheet](/upload/InfoBanner/zyjiaocheng/1250/9d1f393f54b44a08bae31e3289f0f58d.jpg)
/// <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(); // } //}#endregionfor (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();//垃圾回收 }
原文:http://www.cnblogs.com/110abcd/p/4971820.html
内容总结
以上是互联网集市为您收集整理的winform 向excel中追加sheet全部内容,希望文章能够帮你解决winform 向excel中追加sheet所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。