Dataset 转 XLSX
Dataset 转 XLSX
public string DataSetToXlsx(DataSet vContent, string vOutputFilePath) { string result; try { if (vContent != null && vContent.Tables.Count > 0) { using (SLDocument sldocument = new SLDocument()) { if (File.Exists(vOutputFilePath)) { File.Delete(vOutputFilePath); } foreach (object obj in vContent.Tables) { DataTable dataTable = (DataTable)obj; sldocument.AddWorksheet(dataTable.TableName); sldocument.ImportDataTable(1, 1, dataTable, true); for (int i = 0; i < dataTable.Columns.Count; i++) { SLStyle slstyle = sldocument.CreateStyle(); if (dataTable.Columns[i].DataType.FullName.Equals("System.String")) { slstyle.FormatCode = "@"; } else if (dataTable.Columns[i].DataType.FullName.Equals("System.DateTime")) { slstyle.FormatCode = "yyyy/mm/dd hh:mm:ss"; } else if (dataTable.Columns[i].DataType.FullName.Equals("System.Int16")) { slstyle.FormatCode = "#"; } else if (dataTable.Columns[i].DataType.FullName.Equals("System.Int32")) { slstyle.FormatCode = "#"; } else if (dataTable.Columns[i].DataType.FullName.Equals("System.Int64")) { slstyle.FormatCode = "#"; } else { slstyle.FormatCode = "General"; } sldocument.SetColumnStyle(i + 1, slstyle); sldocument.AutoFitColumn(i + 1, dataTable.Columns.Count, 300.0); } SLTable sltable = sldocument.CreateTable(1, 1, dataTable.Rows.Count + 1, dataTable.Columns.Count); sltable.SetTableStyle(SLTableStyleTypeValues.Medium1); sldocument.InsertTable(sltable); } sldocument.DeleteWorksheet("Sheet1"); sldocument.SaveAs(vOutputFilePath); } result = "OK"; } else { result = "DataSet is null"; } } catch (Exception ex) { throw new Exception("DataSet to Xlsx : \n" + ex.Message); } return result; }
本文来自博客园,作者:码农阿亮,转载请注明原文链接:https://www.cnblogs.com/wml-it/p/14731140.html
技术的发展日新月异,随着时间推移,无法保证本博客所有内容的正确性。如有误导,请大家见谅,欢迎评论区指正!
开源库地址,欢迎点亮:
GitHub:https://github.com/ITMingliang
Gitee: https://gitee.com/mingliang_it
GitLab: https://gitlab.com/ITMingliang
建群声明: 本着技术在于分享,方便大家交流学习的初心,特此建立【编程内功修炼交流群】,为大家答疑解惑。热烈欢迎各位爱交流学习的程序员进群,也希望进群的大佬能不吝分享自己遇到的技术问题和学习心得!进群方式:扫码关注公众号,后台回复【进群】。