datagridview中将数据导入到Excel中
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable("cart");
DataColumn dc1 = new DataColumn("areaid", Type.GetType("System.String"));
DataColumn dc2 = new DataColumn("house", Type.GetType("System.String"));
DataColumn dc3 = new DataColumn("seq", Type.GetType("System.String"));
DataColumn dc4 = new DataColumn("remark", Type.GetType("System.String"));
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
dt.Columns.Add(dc4);
DataRow dr = dt.NewRow();
dr["areaid"] = "北京";
dr["house"] = "北京";
dr["seq"] = "2";
dr["remark"] = "货付款";
dt.Rows.Add(dr);
DataRow dr1 = dt.NewRow();
dr1["areaid"] = "北京";
dr1["house"] = "上海";
dr1["seq"] = "1";
dr1["remark"] = "货付款";
dt.Rows.Add(dr1);
DataRow dr2 = dt.NewRow();
dr2["areaid"] = "上海";
dr2["house"] = "上海";
dr2["seq"] = "1";
dr2["remark"] = "货付款";
dt.Rows.Add(dr2);
DataRow dr3 = dt.NewRow();
dr3["areaid"] = "上海";
dr3["house"] = "北京";
dr3["seq"] = "1";
dr3["remark"] = "付款";
dt.Rows.Add(dr3);
string[] columns = {"城市","名称","顺序","备注"};
OutputXLSFromDataTable(columns, dt, saveFileDialog1);
}
/// <summary>
/// 导出DataTable为XLS,并打开生成的XLS
/// </summary>
/// <param name="columns">列名</param>
/// <param name="dt">表</param>
/// <param name="saveFileDialog">对话框</param>
/// <returns></returns>
public static bool OutputXLSFromDataTable(string[] columns, DataTable dt, SaveFileDialog saveFileDialog)
{
DialogResult rs = saveFileDialog.ShowDialog();
if (rs != DialogResult.OK)
{
return false;
}
try
{
ExcelWriter excel = new ExcelWriter(saveFileDialog.FileName);
excel.BeginWrite();
short cols = 0;
if (columns == null || columns.Length == 0)
{//若没有传列名,则以dt的列名做为Excel的列名
foreach (DataColumn col in dt.Columns)
{
excel.WriteString(0, cols, col.ColumnName);
cols++;
}
}
else
{
foreach (string column in columns)
{
excel.WriteString(0, cols, column);
cols++;
}
}
short rows = 1;
foreach (DataRow dr in dt.Rows)
{
cols = 0;
foreach (DataColumn col in dt.Columns)
{
excel.WriteString(rows, cols, dr[col].ToString());
cols++;
}
rows++;
}
excel.EndWrite();
Process.Start(saveFileDialog.FileName);
return true;
}
catch
{
return false;
}
}
}
/// <summary>
/// 通过文件结构直接生成xls文件
/// </summary>
public class ExcelWriter
{
FileStream _wirter;
public ExcelWriter(string strPath)
{
_wirter = new FileStream(strPath, FileMode.OpenOrCreate);
}
/// <summary>
/// 写入short数组
/// </summary>
/// <param name="values"></param>
private void _writeFile(short[] values)
{
foreach (short v in values)
{
byte[] b = BitConverter.GetBytes(v);
_wirter.Write(b, 0, b.Length);
}
}
/// <summary>
/// 写文件头
/// </summary>
public void BeginWrite()
{
_writeFile(new short[] { 0x809, 8, 0, 0x10, 0, 0 });
}
/// <summary>
/// 写文件尾
/// </summary>
public void EndWrite()
{
_writeFile(new short[] { 0xa, 0 });
_wirter.Close();
}
/// <summary>
/// 写一个数字到单元格x,y
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="value"></param>
public void WriteNumber(short x, short y, double value)
{
_writeFile(new short[] { 0x203, 14, x, y, 0 });
byte[] b = BitConverter.GetBytes(value);
_wirter.Write(b, 0, b.Length);
}
/// <summary>
/// 写一个字符到单元格x,y
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="value"></param>
public void WriteString(short x, short y, string value)
{
byte[] b = Encoding.Default.GetBytes(value);
_writeFile(new short[] { 0x204, (short)(b.Length + 8), x, y, 0, (short)b.Length });
_wirter.Write(b, 0, b.Length);
}
}