123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491 |
- using NPOI.HSSF.UserModel;
- using NPOI.SS.UserModel;
- using NPOI.XSSF.UserModel;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Diagnostics;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Windows.Forms;
- namespace WWPipeLine.Commons
- {
- public class ExportDgvToExcel
- {
-
-
-
- public static readonly string CANCELRESULTSTRING = "Cancel";
- public static string ExportExcel(List<DataGridView> dgvs)
- {
- if (dgvs.Count == 0) { return ("内容为空!"); }
- string saveFileName = "";
- SaveFileDialog sfd = new SaveFileDialog
- {
- DefaultExt = "xlsx",
- Filter = "Excel文件(*.xlsx)|*.xlsx",
- RestoreDirectory = true,
- Title = "Excel文件保存路径"
- };
- if (sfd.ShowDialog() == DialogResult.Cancel) return CANCELRESULTSTRING;
- saveFileName = sfd.FileName;
- if (!CheckFiles(saveFileName)) return "文件被占用,请关闭文件";
- MemoryStream ms = new MemoryStream();
- XSSFWorkbook workbook = new XSSFWorkbook();
- if (workbook == null) return "生成IWorkbook失败";
-
- ICellStyle cellStyle = workbook.CreateCellStyle();
-
- cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
- cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
-
- IFont font = workbook.CreateFont();
- font.FontName = "雅黑";
- font.FontHeightInPoints = 12;
- font.Color = NPOI.HSSF.Util.HSSFColor.Black.Index;
- cellStyle.SetFont(font);
- foreach (DataGridView dgv in dgvs.ToArray())
- {
- if (dgv.Rows.Count > 1000000) { return ("行数超过Excel限制!"); }
- string sheetName = "S" + DateTime.Now.Ticks;
- if (dgv.Tag != null)
- sheetName = dgv.Tag.ToString();
- ISheet sheet = workbook.CreateSheet(sheetName);
-
- IRow headRow = sheet.CreateRow(0);
- var cIndex = 0;
- Dictionary<int, int> columnsVisiable = new Dictionary<int, int>();
- foreach (DataGridViewColumn column in dgv.Columns)
- {
- if (!column.Visible) continue;
- headRow.CreateCell(cIndex).SetCellValue(column.HeaderText);
- headRow.GetCell(cIndex).CellStyle = cellStyle;
- columnsVisiable.Add(cIndex, dgv.Columns.IndexOf(column));
- cIndex++;
- }
-
- for (int rowNum = 0; rowNum < dgv.Rows.Count; rowNum++)
- {
-
- IRow dataRow = sheet.CreateRow(rowNum + 1);
- foreach (int columnIndex in columnsVisiable.Keys)
- {
- int columnNum = columnsVisiable[columnIndex];
- ICell cell = dataRow.CreateCell(columnIndex);
- if (dgv.Rows[rowNum].Cells[columnNum].Value == null)
- {
- cell.SetCellType(CellType.Blank); continue;
- }
- if (dgv.Rows[rowNum].Cells[columnNum].ValueType.FullName.Contains("System.Int32"))
- {
- cell.SetCellValue(Convert.ToInt32(dgv.Rows[rowNum].Cells[columnNum].Value));
- }
- else if (dgv.Rows[rowNum].Cells[columnNum].ValueType.FullName.Contains("System.String"))
- {
- cell.SetCellValue(dgv.Rows[rowNum].Cells[columnNum].Value.ToString());
- }
- else if (dgv.Rows[rowNum].Cells[columnNum].ValueType.FullName.Contains("System.Single"))
- {
- cell.SetCellValue(Convert.ToSingle(dgv.Rows[rowNum].Cells[columnNum].Value));
- }
- else if (dgv.Rows[rowNum].Cells[columnNum].ValueType.FullName.Contains("System.Double"))
- {
- cell.SetCellValue(Convert.ToDouble(dgv.Rows[rowNum].Cells[columnNum].Value));
- }
- else if (dgv.Rows[rowNum].Cells[columnNum].ValueType.FullName.Contains("System.Decimal"))
- {
- cell.SetCellValue(Convert.ToDouble(dgv.Rows[rowNum].Cells[columnNum].Value));
- }
- else if (dgv.Rows[rowNum].Cells[columnNum].ValueType.FullName.Contains("System.DateTime"))
- {
- cell.SetCellValue(Convert.ToDateTime(dgv.Rows[rowNum].Cells[columnNum].Value).ToString("yyyy-MM-dd"));
- }
- else if (dgv.Rows[rowNum].Cells[columnNum].ValueType.FullName.Contains("System.DBNull"))
- {
- cell.SetCellValue("");
- }
-
- dataRow.GetCell(columnIndex).CellStyle = cellStyle;
- }
- }
- }
- if (!ms.CanRead) return string.Empty;
-
- workbook.Write(ms);
- using (FileStream file = new FileStream(saveFileName, FileMode.Create))
- {
- workbook.Write(file);
- file.Close();
- }
- ms.Close();
- ms.Dispose();
- LogHelper.Info("导出成功");
- return string.Empty;
- }
- public static string ExportExcel(DataGridView dgv)
- {
- if (dgv.Rows.Count == 0) { return ("内容为空!"); }
- if (dgv.Rows.Count > 1000000) { return ("行数超过Excel限制!"); }
- string saveFileName = "";
- SaveFileDialog sfd = new SaveFileDialog
- {
- DefaultExt = "xlsx",
- Filter = "Excel文件(*.xlsx)|*.xlsx",
- RestoreDirectory = true,
- Title = "Excel文件保存路径"
- };
- if (sfd.ShowDialog() == DialogResult.Cancel) return CANCELRESULTSTRING;
- saveFileName = sfd.FileName;
- if (!CheckFiles(saveFileName)) return "文件被占用,请关闭文件";
- MemoryStream ms = new MemoryStream();
- XSSFWorkbook workbook = new XSSFWorkbook();
- if (workbook == null) return "生成IWorkbook失败";
-
- ICellStyle cellStyle = workbook.CreateCellStyle();
-
- cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
- cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
-
- IFont font = workbook.CreateFont();
- font.FontName = "雅黑";
- font.FontHeightInPoints = 12;
- font.Color = NPOI.HSSF.Util.HSSFColor.Black.Index;
- cellStyle.SetFont(font);
- string sheetName = "S" + DateTime.Now.Ticks;
- if (dgv.Tag != null)
- sheetName = dgv.Tag.ToString();
- ISheet sheet = workbook.CreateSheet(sheetName);
-
- IRow headRow = sheet.CreateRow(0);
- var cIndex = 0;
- Dictionary<int, int> columnsVisiable = new Dictionary<int, int>();
- foreach (DataGridViewColumn column in dgv.Columns)
- {
- if (!column.Visible) continue;
- headRow.CreateCell(cIndex).SetCellValue(column.HeaderText);
- headRow.GetCell(cIndex).CellStyle = cellStyle;
- columnsVisiable.Add(cIndex, dgv.Columns.IndexOf(column));
- cIndex++;
- }
-
- for (int rowNum = 0; rowNum < dgv.Rows.Count; rowNum++)
- {
-
- IRow dataRow = sheet.CreateRow(rowNum + 1);
- foreach (int columnIndex in columnsVisiable.Keys)
- {
- int columnNum = columnsVisiable[columnIndex];
- ICell cell = dataRow.CreateCell(columnIndex);
- if (dgv.Rows[rowNum].Cells[columnNum].Value == null)
- {
- cell.SetCellType(CellType.Blank); continue;
- }
- if (dgv.Rows[rowNum].Cells[columnNum].ValueType.FullName.Contains("System.Int32"))
- {
- cell.SetCellValue(Convert.ToInt32(dgv.Rows[rowNum].Cells[columnNum].Value));
- }
- else if (dgv.Rows[rowNum].Cells[columnNum].ValueType.FullName.Contains("System.String"))
- {
- cell.SetCellValue(dgv.Rows[rowNum].Cells[columnNum].Value.ToString());
- }
- else if (dgv.Rows[rowNum].Cells[columnNum].ValueType.FullName.Contains("System.Single"))
- {
- cell.SetCellValue(Convert.ToSingle(dgv.Rows[rowNum].Cells[columnNum].Value));
- }
- else if (dgv.Rows[rowNum].Cells[columnNum].ValueType.FullName.Contains("System.Double"))
- {
- cell.SetCellValue(Convert.ToDouble(dgv.Rows[rowNum].Cells[columnNum].Value));
- }
- else if (dgv.Rows[rowNum].Cells[columnNum].ValueType.FullName.Contains("System.Decimal"))
- {
- cell.SetCellValue(Convert.ToDouble(dgv.Rows[rowNum].Cells[columnNum].Value));
- }
- else if (dgv.Rows[rowNum].Cells[columnNum].ValueType.FullName.Contains("System.DateTime"))
- {
- cell.SetCellValue(Convert.ToDateTime(dgv.Rows[rowNum].Cells[columnNum].Value).ToString("yyyy-MM-dd"));
- }
- else if (dgv.Rows[rowNum].Cells[columnNum].ValueType.FullName.Contains("System.DBNull"))
- {
- cell.SetCellValue("");
- }
-
- dataRow.GetCell(columnIndex).CellStyle = cellStyle;
- }
- }
- if (!ms.CanRead) return string.Empty;
-
- workbook.Write(ms);
- using (FileStream file = new FileStream(saveFileName, FileMode.Create))
- {
- workbook.Write(file);
- file.Close();
- }
- ms.Close();
- ms.Dispose();
- LogHelper.Info("导出成功");
- return string.Empty;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #region 检测文件是否被占用
-
-
-
- [DllImport("kernel32.dll")]
- public static extern IntPtr Lopen(string lpPathName, int iReadWrite);
- [DllImport("kernel32.dll")]
- public static extern bool CloseHandle(IntPtr hObject);
- public const int OF_READWRITE = 2;
- public const int OF_SHARE_DENY_NONE = 0x40;
- public static readonly IntPtr HFILE_ERROR = new IntPtr(-1);
-
-
-
-
-
- public static bool CheckFiles(string FileNames)
- {
- if (!File.Exists(FileNames)) return true;
- try
- {
- IntPtr vHandle = Lopen(FileNames, OF_READWRITE | OF_SHARE_DENY_NONE);
- if (vHandle == HFILE_ERROR)
- return false;
-
- CloseHandle(vHandle);
- }
- catch { return false; }
- return true;
- }
- #endregion
- }
- }
|