文本文件txt生成excel
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Data.OleDb; using System.Data; using System.Windows.Forms; using NPOI; using NPOI.Util; using NPOI.HPSF; using NPOI.SS.UserModel; using NPOI.HSSF.UserModel; using NPOI.HSSF; using NPOI.HSSF.Util; using NPOI.SS.Util; namespace CSManager.Class { public class TxtToShp { public TxtToShp() { } public static void addText(string filepath, string outPath) { var reader = new StreamReader(filepath); var list = new List<string[]>(); int count = 0; string[,] M = null; //读取第一行属性行 var line = reader.ReadLine().Split(new[] { "\t" }, StringSplitOptions.RemoveEmptyEntries); var arr = new string[line.Length]; for (var O = 0; O < line.Length; O++) arr[O] = line[O]; list.Add(arr); while (!reader.EndOfStream) { count++; var line1 = reader.ReadLine().Split(new[] { "\t" }, StringSplitOptions.RemoveEmptyEntries); var arr1 = new string[line1.Length]; if (line1.Length != line.Length) { MessageBox.Show("数据文件第" + count.ToString() + "行不符合要求,请检查!"); return; } else { for (var O = 0; O < line1.Length; O++) arr1[O] = line1[O]; list.Add(arr1); } } var SJD = list.ToArray(); M = new string[SJD.Length, line.Length]; for (int i = 0; i < SJD.Length; i++) { for (int j = 0; j < line.Length; j++) { M[i, j] = SJD[i][j]; } } System.Data.DataTable dt = Convert(M); ExportExcel(dt, outPath); } public static void ExportExcel(System.Data.DataTable dtSource, string strFileName) { using (MemoryStream ms = Export(dtSource)) { using (FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write)) { byte[] data = ms.ToArray(); fs.Write(data, 0, data.Length); fs.Flush(); } } } /// <summary> /// DataTable导出到Excel的MemoryStream /// </summary> /// <param name="dtSource">源DataTable</param> /// <param name="strHeaderText">表头文本</param> public static MemoryStream Export(DataTable dtSource) { HSSFWorkbook workbook = new NPOI.HSSF.UserModel.HSSFWorkbook(); NPOI.SS.UserModel.ISheet sheet = workbook.CreateSheet(); #region 右击文件 属性信息 { DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation(); dsi.Company = "NPOI"; workbook.DocumentSummaryInformation = dsi; SummaryInformation si = PropertySetFactory.CreateSummaryInformation(); si.Author = "文件作者信息"; //填加xls文件作者信息 si.ApplicationName = "创建程序信息"; //填加xls文件创建程序信息 si.LastAuthor = "最后保存者信息"; //填加xls文件最后保存者信息 si.Comments = "作者信息"; //填加xls文件作者信息 si.Title = "标题信息"; //填加xls文件标题信息 si.Subject = "主题信息";//填加文件主题信息 si.CreateDateTime = DateTime.Now; workbook.SummaryInformation = si; } #endregion ICellStyle dateStyle = workbook.CreateCellStyle(); IDataFormat format = workbook.CreateDataFormat(); dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd"); //取得列宽 int[] arrColWidth = new int[dtSource.Columns.Count]; foreach (DataColumn item in dtSource.Columns) { arrColWidth[item.Ordinal] = Encoding.GetEncoding(936).GetBytes(item.ColumnName.ToString()).Length; } for (int i = 0; i < dtSource.Rows.Count; i++) { for (int j = 0; j < dtSource.Columns.Count; j++) { int intTemp = Encoding.GetEncoding(936).GetBytes(dtSource.Rows[i][j].ToString()).Length; if (intTemp > arrColWidth[j]) { arrColWidth[j] = intTemp; } } } int rowIndex = 0; foreach (DataRow row in dtSource.Rows) { #region 填充内容 IRow dataRow = sheet.CreateRow(rowIndex); foreach (DataColumn column in dtSource.Columns) { ICell newCell = dataRow.CreateCell(column.Ordinal); string drValue = row[column].ToString(); switch (column.DataType.ToString()) { case "System.String"://字符串类型 newCell.SetCellValue(drValue); break; case "System.DateTime"://日期类型 DateTime dateV; DateTime.TryParse(drValue, out dateV); newCell.SetCellValue(dateV); newCell.CellStyle = dateStyle;//格式化显示 break; case "System.Boolean"://布尔型 bool boolV = false; bool.TryParse(drValue, out boolV); newCell.SetCellValue(boolV); break; case "System.Int16"://整型 case "System.Int32": case "System.Int64": case "System.Byte": int intV = 0; int.TryParse(drValue, out intV); newCell.SetCellValue(intV); break; case "System.Decimal"://浮点型 case "System.Double": double doubV = 0; double.TryParse(drValue, out doubV); newCell.SetCellValue(doubV); break; case "System.DBNull"://空值处理 newCell.SetCellValue(""); break; default: newCell.SetCellValue(""); break; } } #endregion rowIndex++; } using (MemoryStream ms = new MemoryStream()) { workbook.Write(ms); ms.Flush(); ms.Position = 0; return ms; } } /// <summary> /// 反一个M行N列的二维数组转换为DataTable /// </summary> /// <param name="Arrays">M行N列的二维数组</param> /// <returns>返回DataTable</returns> public static System.Data.DataTable Convert(string[,] Arrays) { System.Data.DataTable dt = new System.Data.DataTable(); int a = Arrays.GetLength(0); for (int i = 0; i < Arrays.GetLength(1); i++) { dt.Columns.Add(Arrays[0,i].ToString(), typeof(string)); } for (int i1 = 0; i1 < Arrays.GetLength(0); i1++) { DataRow dr = dt.NewRow(); for (int i = 0; i < Arrays.GetLength(1); i++) { dr[i] = Arrays[i1, i].ToString(); } dt.Rows.Add(dr); } return dt; } } }
作者:jinqier
出处:http://www.cnblogs.com/jinqier/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。