C# 操作Excel多个sheet的具体的操作是什么呢?让我们来看看下面的实例实现:
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名申请、网站空间、营销软件、网站建设、汝阳网站维护、网站推广。
- private void DataViewExcelBySheetMultipleDt(
- DataSet ds, string fileName)
- {
- try
- {
- int sheetCount = ds.Tables.Count;
- GC.Collect();
- Application excel;
- _Workbook xBk;
- _Worksheet xSt = null;
- excel = new ApplicationClass();
- xBk = excel.Workbooks.Add(true);
- int rowIndex = 0;
- int colIndex = 0;
- for (int sheetIndex = 0;
- sheetIndex < sheetCount; sheetIndex++)
- {
- rowIndex = 1;
- colIndex = 1;
- xSt = (_Worksheet)xBk.Worksheets.Add(
- Type.Missing, Type.Missing, 1, Type.Missing);
- switch (sheetIndex)
- {
- case 0:
- xSt.Name = "test1";
- break;
- case 1:
- xSt.Name = "test2";
- break;
- case 2:
- xSt.Name = "test3";
- break;
- case 3:
- xSt.Name = "test4";
- break;
- } //C# 操作Excel多个sheet的具体的操作
- foreach (DataColumn col in ds.Tables[sheetIndex].Columns)
- {
- xSt.get_Range(excel.Cells[rowIndex,
- colIndex], excel.Cells[rowIndex,
- colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;
- xSt.get_Range(excel.Cells[rowIndex,
- colIndex], excel.Cells[rowIndex,
- colIndex]).Font.Bold = true;
- excel.Cells[rowIndex, colIndex++] = col.ColumnName;
- }
- foreach (DataRow row in ds.
- Tables[sheetIndex].Rows)
- {
- rowIndex++;
- colIndex = 1;
- foreach (DataColumn col in ds.
- Tables[sheetIndex].Columns)
- {
- if (col.DataType == System.Type.GetType(
- "System.DateTime"))
- {
- if (!"".Equals(row[col.ColumnName].ToString()))
- excel.Cells[rowIndex, colIndex] =
- (Convert.ToDateTime(row[col.ColumnName].
- ToString())).ToString("MM/dd/yyyy");
- else
- excel.Cells[rowIndex, colIndex] = "";
- }
- else if (col.DataType == S
- ystem.Type.GetType("System.String"))
- {
- excel.Cells[rowIndex, colIndex] = "'" +
- row[col.ColumnName].ToString();
- }
- else
- {
- excel.Cells[rowIndex, colIndex] =
- row[col.ColumnName].ToString();
- }
- colIndex++;
- } //C# 操作Excel多个sheet的具体的操作
- }
- Range allDataWithTitleRange = xSt.get_Range(
- excel.Cells[1, 1], excel.Cells[rowIndex, colIndex - 1]);
- allDataWithTitleRange.Select();
- allDataWithTitleRange.Columns.AutoFit();
- allDataWithTitleRange.Borders.LineStyle = 1;
- }
- string exportDir = "~/Attachment/";
- string absFileName = HttpContext.
- Current.Server.MapPath(
- System.IO.Path.Combine(exportDir, fileName));
- xBk.SaveCopyAs(absFileName);
- xBk.Close(false, null, null);
- excel.Quit();
- System.Runtime.InteropServices.
- Marshal.ReleaseComObject(xBk);
- System.Runtime.InteropServices.
- Marshal.ReleaseComObject(excel);
- System.Runtime.InteropServices.
- Marshal.ReleaseComObject(xSt);
- //C# 操作Excel多个sheet的具体的操作
- xBk = null;
- excel = null;
- xSt = null;
- GC.Collect();
- }
- catch (Exception ex)
- {
- }
- }
- private void DataViewExcelBySheetMultipleDt(
- DataSet ds, string fileName)
- {
- try
- {
- int sheetCount = ds.Tables.Count;
- GC.Collect();
- Application excel;
- _Workbook xBk;
- _Worksheet xSt = null;
- excel = new ApplicationClass();
- xBk = excel.Workbooks.Add(true);
- //C# 操作Excel多个sheet的具体的操作
- int rowIndex = 0;
- int colIndex = 0;
- for (int sheetIndex = 0;
- sheetIndex < sheetCount; sheetIndex++)
- {
- rowIndex = 1;
- colIndex = 1;
- xSt = (_Worksheet)xBk.Worksheets.Add(
- Type.Missing, Type.Missing, 1, Type.Missing);
- switch (sheetIndex)
- {
- case 0:
- xSt.Name = "test1";
- break;
- case 1:
- xSt.Name = "test2";
- break;
- case 2:
- xSt.Name = "test3";
- break;
- case 3:
- xSt.Name = "test4";
- break;
- }
- foreach (DataColumn col in ds.Tables[sheetIndex].Columns)
- { //C# 操作Excel多个sheet的具体的操作
- xSt.get_Range(excel.Cells[rowIndex,
- colIndex], excel.Cells[rowIndex,
- colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;
- xSt.get_Range(excel.Cells[rowIndex,
- colIndex], excel.Cells[rowIndex, colIndex]).Font.Bold = true;
- excel.Cells[rowIndex, colIndex++] = col.ColumnName;
- }
- foreach (DataRow row in ds.Tables[sheetIndex].Rows)
- {
- rowIndex++;
- colIndex = 1;
- foreach (DataColumn col in ds.Tables[
- sheetIndex].Columns)
- {
- if (col.DataType == System.Type.GetType(
- "System.DateTime"))
- {
- if (!"".Equals(row[col.ColumnName].ToString()))
- excel.Cells[rowIndex, colIndex] = (
- Convert.ToDateTime(row[col.ColumnName].
- ToString())).ToString("MM/dd/yyyy");
- else
- excel.Cells[rowIndex, colIndex] = "";
- }
- else if (col.DataType ==
- System.Type.GetType("System.String"))
- {
- excel.Cells[rowIndex,
- colIndex] = "'" + row[col.ColumnName].ToString();
- }
- else
- {
- excel.Cells[rowIndex,
- colIndex] = row[col.ColumnName].ToString();
- }
- colIndex++;
- }
- } //C# 操作Excel多个sheet的具体的操作
- Range allDataWithTitleRange = xSt.get_Range(
- excel.Cells[1, 1],
- excel.Cells[rowIndex, colIndex - 1]);
- allDataWithTitleRange.Select();
- allDataWithTitleRange.Columns.AutoFit();
- allDataWithTitleRange.Borders.LineStyle = 1;
- }
- string exportDir = "~/Attachment/";
- string absFileName = HttpContext.Current.Server.
- MapPath(System.IO.Path.Combine(exportDir, fileName));
- xBk.SaveCopyAs(absFileName);
- xBk.Close(false, null, null);
- excel.Quit();
- System.Runtime.InteropServices.
- Marshal.ReleaseComObject(xBk);
- System.Runtime.InteropServices.
- Marshal.ReleaseComObject(excel);
- System.Runtime.InteropServices.
- Marshal.ReleaseComObject(xSt);
- xBk = null;
- excel = null;
- xSt = null;
- GC.Collect();
- }
- catch (Exception ex)
- {
- }
- }
上面方法,首先形成一个多个DataTable的DataSet,
C# 操作Excel重点还是
1. 生成一个新的xls时,打开方式,总是会提示进程占用
2. 用不同的sheet时一定要命名
3. 使用传入一个datatable时,总是会重写第一个sheet
C# 操作Excel多个sheet的具体的操作实例就向你介绍到这里,希望对你了解和学习C# 操作Excel多个sheet的具体的操作有所帮助。
分享标题:C#操作Excel实例浅析
网址分享:http://www.shufengxianlan.com/qtweb/news21/282471.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联