Files
29WxOrder/AllBase/OutExcel.cs
T
2026-06-11 15:17:50 +08:00

195 lines
6.3 KiB
C#

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
{
/// <summary>
/// 保存数据
/// </summary>
/// <param name="patch"></param>
/// <param name="dt"></param>
/// <returns></returns>
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<object> values = new List<object>();
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 导出
/// <summary>
/// 数据导出
/// </summary>
/// <param name="data"></param>
/// <param name="sheetName"></param>
public static void ExportToExcelData(this DataTable data, string fileName = "")
{
ExportToExcel(data, "Sheet1", fileName);
}
/// <summary>
/// 数据导出
/// </summary>
/// <param name="data"></param>
/// <param name="sheetName"></param>
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
}
}