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