做项目过程中,导出项目碰到excel版本问题屡见不鲜,这个时候按照一般方法已经没办法解决了(个人水平有限),于是找度娘,发现有种NPOI导出excel的方法
就是引用NPOI.DLL,可以不管版本问题了。
完整代码如下,读取文件至datatable,再导出excel
路径读取:
private void button1_Click(object sender, EventArgs e)//选取文件 { OpenFileDialog openFileDialog1 = new OpenFileDialog(); if (openFileDialog1.ShowDialog() == DialogResult.OK) { if (openFileDialog1.FileName != "") { this.textBox1.Text = openFileDialog1.FileName; } } }
主类:
private void button6_Click(object sender, EventArgs e) { DataTable dataTable = new DataTable(); dataTable = Csv2DT.csv2dt(this.textBox1.Text, 0, dataTable);//取文本框中的path String Text = dataTable.Rows[1]["物料编码"].ToString();//用于测试是不是读取成功 NPOIoutputexcel.ExportToExcelData(dataTable, "sheet1"); }
转为datatable类如下
class Csv2DT { /// <summary> /// 将Csv读入DataTable /// </summary> /// <param name="filePath">csv文件路径</param> /// <param name="n">表示第n行是字段title,第n+1行是记录开始</param> /// <param name="k">可选参数表示最后K行不算记录默认0</param> public static DataTable csv2dt(string filePath, int n, DataTable dt) //这个dt 是个空白的没有任何行列的DataTable { String csvSplitBy = "(?<=^|,)(\"(?:[^\"]|\"\")*\"|[^,]*)"; StreamReader reader = new StreamReader(filePath, System.Text.Encoding.Default, false); int i = 0, m = 0; reader.Peek(); while (reader.Peek() > 0) { m = m + 1; string str = reader.ReadLine(); if (m >= n + 1) { if (m == n + 1) //如果是字段行,则自动加入字段。 { MatchCollection mcs = Regex.Matches(str, csvSplitBy); foreach (Match mc in mcs) { dt.Columns.Add(mc.Value); //增加列标题 } } else { MatchCollection mcs = Regex.Matches(str, "(?<=^|,)(\"(?:[^\"]|\"\")*\"|[^,]*)"); i = 0; System.Data.DataRow dr = dt.NewRow(); foreach (Match mc in mcs) { dr[i] = mc.Value; i++; } dt.Rows.Add(dr); //DataTable 增加一行 } } } return dt; } }
NPOI导出excel,先要引用NPOI.DLL,可以网上下载
static class NPOIoutputexcel { public static void ExportToExcelData(DataTable DataTable, string sheetName) { SaveFileDialog fileDialog = new SaveFileDialog(); //fileDialog.Filter = "Excel(97-2003)|*.xls|Excel(2007-2013)|*.xlsx"; fileDialog.Filter = "Excel|*.xls|Excel|*.xlsx"; if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) { return; } //IWorkbook workbook = new XSSFWorkbook(); //1、基于NPOI创建工作簿及工作表对象 HSSFWorkbook hssf = new HSSFWorkbook();//2007以下版本 //XSSFWorkbook xssf = new XSSFWorkbook();//2007以上版本 //根据不同的excel版本创建不同干的工作簿 IWorkbook workbook = null; workbook = hssf; ISheet sheet = workbook.CreateSheet(sheetName); IRow rowHead = sheet.CreateRow(0); //填写表头 for (int i = 0; i < DataTable.Columns.Count; i++) { rowHead.CreateCell(i, CellType.String).SetCellValue(DataTable.Columns[i].ColumnName.ToString()); } //填写内容 for (int i = 0; i < DataTable.Rows.Count; i++) { IRow row = sheet.CreateRow(i + 1); for (int j = 0; j < DataTable.Columns.Count; j++) { row.CreateCell(j, CellType.String).SetCellValue(DataTable.Rows[i][j].ToString()); } } for (int i = 0; i < DataTable.Columns.Count; i++) { sheet.AutoSizeColumn(i); } using (FileStream stream = File.OpenWrite(fileDialog.FileName)) { workbook.Write(stream); stream.Close(); } MessageBox.Show("导出数据成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); GC.Collect(); } }