using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Text; using System.Windows.Forms; namespace AllBase { public static class OutExcel { /// /// 保存数据 /// /// /// /// public static bool SaveFile(string patch, DataGridView dt) { string temp = ""; for (int i = 0; i < dt.ColumnCount; i++) { if (dt.Columns[i].HeaderText != "删除") temp += dt.Columns[i].HeaderText + "\t"; } if (!string.IsNullOrEmpty(temp)) temp += "\r\n"; try { if (File.Exists(patch)) File.Delete(patch); File.WriteAllText(patch, temp, Encoding.Unicode); temp = ""; } catch { return false; } for (int i = 0; i < dt.RowCount; i++) { if (dt.Rows[i].Selected != true) { break; } for (int j = 0; j < dt.ColumnCount; j++) { if (dt.Columns[j].HeaderText != "删除" && dt.Rows[i].Cells[j] == null) { temp += dt.Rows[i].Cells[j].Value.ToString() + "\t"; } } temp += "\r\n"; if (temp.Length > 2000) { try { File.AppendAllText(patch, temp, Encoding.Unicode); temp = ""; } catch { //return false; } } } if (string.IsNullOrEmpty(temp)) return true; try { File.AppendAllText(patch, temp, Encoding.Unicode); } catch { return false; } return true; } public static void ExportToExcel(DataTable dt, string fileName = "") { ExportToExcelData(dt, fileName); } public static DataTable ToDataTable(this DataGridView myDGV) { DataTable dt = new DataTable(); for (int i = 0; i < myDGV.ColumnCount; i++) { if (myDGV.Columns[i].HeaderText != "图片" && myDGV.Columns[i].HeaderText != "开始拍单" /*&& myDGV.Columns[i].HeaderText != "后台备注"*/) { dt.Columns.Add(myDGV.Columns[i].HeaderText); } } //写入数值 for (int r = 0; r < myDGV.Rows.Count; r++) { if (myDGV.SelectedRows.Count > 1) { if (myDGV.Rows[r].Selected == false) { continue; } } List values = new List(); for (int i = 0; i < myDGV.ColumnCount; i++) { if (myDGV.Columns[i].HeaderText != "图片" && myDGV.Columns[i].HeaderText != "开始拍单" /*&& myDGV.Columns[i].HeaderText != "后台备注"*/) { values.Add(myDGV.Rows[r].Cells[i].Value); } } dt.Rows.Add(values.ToArray()); } return dt; } #region 导出 /// /// 数据导出 /// /// /// public static void ExportToExcelData(this DataTable data, string fileName = "") { ExportToExcel(data, "Sheet1", fileName); } /// /// 数据导出 /// /// /// public static void ExportToExcel(this DataTable data, string sheetName, string fileName = "") { bool isFactoryOrder = !string.IsNullOrEmpty(fileName); SaveFileDialog fileDialog = new SaveFileDialog(); //fileDialog.Filter = "Excel(97-2003)|*.xls|Excel(2007-2013)|*.xlsx"; fileDialog.Filter = "Excel|*.xls|Excel|*.xlsx"; fileName += DateTime.Now.ToString("yyyyMMddHHmmss"); fileDialog.FileName = fileName; if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) { return; } IWorkbook workbook = new XSSFWorkbook(); ISheet sheet = workbook.CreateSheet(sheetName); IRow rowHead = sheet.CreateRow(0); //填写表头 for (int i = 0; i < data.Columns.Count; i++) { rowHead.CreateCell(i, CellType.String).SetCellValue(data.Columns[i].ColumnName.ToString()); } //填写内容 for (int i = 0; i < data.Rows.Count; i++) { IRow row = sheet.CreateRow(i + 1); for (int j = 0; j < data.Columns.Count; j++) { string cValue = data.Rows[i][j].ToString(); if ((data.Columns[j].ColumnName.ToString() == "姓名" || data.Columns[j].ColumnName.ToString() == "电话" || data.Columns[j].ColumnName.ToString() == "地址") && !isFactoryOrder) { //cValue = "******"; } row.CreateCell(j, CellType.String).SetCellValue(cValue); } } for (int i = 0; i < data.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(); } #endregion } }