/// <summary>
/// 构造函数
/// </summary>
public ExportData()
{
}
/// <summary>
/// 保存文件名
/// </summary>
private string savefilename = "";
/// <summary>
/// 准备导出的数据容器
/// </summary>
private System.Windows.Forms.DataGridView datagv;
/// <summary>
/// 导出excel文档
/// </summary>
/// <param name="d">数据所在控件</param>
public void OutDataToExcelByDataSource(System.Windows.Forms.DataGridView dgview)
{
try
{
if (dgview.Columns.Count < )
{
MessageBox.Show("没有导出数据。");
return;
}
SaveFileDialog sfd = new SaveFileDialog();
sfd.ValidateNames = true;
sfd.Filter = "Excel2003文件(*.xls)|*.xls|Excel2007文件(*.xlsx)|*.xlsx|Access文件(*.mdb)|*.mdb|DBF文件(*.dbf)|*.dbf";
if (sfd.ShowDialog() != DialogResult.OK)
{
return;
}
this.savefilename = sfd.FileName;
//检测文件名是否合法
string tryfile = "";
tryfile = savefilename.Substring(savefilename.LastIndexOf("\\") + );
tryfile = tryfile.Substring(, tryfile.LastIndexOf("."));
foreach (char c in tryfile)
{
if ("<>?:[]|*".Contains(c.ToString()))
{
MessageBox.Show("文件名包含非法字符.");
return;
}
}
this.datagv = dgview;
if (sfd.FileName.Substring(sfd.FileName.LastIndexOf(".")) == ".xlsx" || sfd.FileName.Substring(sfd.FileName.LastIndexOf(".")) == ".xls")
{
outToExcelByDataSource();
}
else if (sfd.FileName.Substring(sfd.FileName.LastIndexOf(".")) == ".mdb" || sfd.FileName.Substring(sfd.FileName.LastIndexOf(".")) == ".dbf")
{
outToAccessByDataSource();
}
}
catch (Exception W)
{
MessageBox.Show(W.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// 导出excel文档
/// </summary>
/// <param name="d">数据所在控件</param>
public void OutDataToExcelByDataTable(System.Windows.Forms.DataGridView dgview)
{
try
{
if (dgview.Columns.Count < )
{
MessageBox.Show("没有导出数据。");
return;
}
SaveFileDialog sfd = new SaveFileDialog();
sfd.ValidateNames = true;
sfd.Filter = "Excel2003文件(*.xls)|*.xls|Excel2007文件(*.xlsx)|*.xlsx|Access文件(*.mdb)|*.mdb|DBF文件(*.dbf)|*.dbf"; ;
if (sfd.ShowDialog() != DialogResult.OK)
{
return;
}
this.savefilename = sfd.FileName;
//检测文件名是否合法
string tryfile = "";
tryfile = savefilename.Substring(savefilename.LastIndexOf("\\") + );
tryfile = tryfile.Substring(, tryfile.LastIndexOf("."));
foreach (char c in tryfile)
{
if ("<>?:[]|*".Contains(c.ToString()))
{
MessageBox.Show("文件名包含非法字符.");
return;
}
}
this.datagv = dgview;
if (sfd.FileName.Substring(sfd.FileName.LastIndexOf(".")).ToLower() == ".xlsx" || sfd.FileName.Substring(sfd.FileName.LastIndexOf(".")).ToLower() == ".xls")
{
outToExcelByDataTable();
}
else if (sfd.FileName.Substring(sfd.FileName.LastIndexOf(".")).ToLower() == ".mdb" || sfd.FileName.Substring(sfd.FileName.LastIndexOf(".")).ToLower() == ".dbf")
{
outToAccessByDataTable();
}
}
catch (Exception W)
{
MessageBox.Show(W.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// 导出文档
/// </summary>
public void OutDataByDataTable(DataTable tab)
{
try
{
if (tab.Columns.Count < )
{
MessageBox.Show("没有导出数据。");
return;
}
SaveFileDialog sfd = new SaveFileDialog();
sfd.ValidateNames = true;
sfd.Filter = "Excel2003文件(*.xls)|*.xls|Excel2007文件(*.xlsx)|*.xlsx|Access文件(*.mdb)|*.mdb|DBF文件(*.dbf)|*.dbf";
if (sfd.ShowDialog() != DialogResult.OK)
{
return;
}
this.savefilename = sfd.FileName;
//检测文件名是否合法
string tryfile = "";
tryfile = savefilename.Substring(savefilename.LastIndexOf("\\") + );
tryfile = tryfile.Substring(, tryfile.LastIndexOf("."));
foreach (char c in tryfile)
{
if ("<>?:[]|*".Contains(c.ToString()))
{
MessageBox.Show("文件名包含非法字符.");
return;
}
}
if (sfd.FileName.Substring(sfd.FileName.LastIndexOf(".")).ToLower() == ".xlsx" || sfd.FileName.Substring(sfd.FileName.LastIndexOf(".")).ToLower() == ".xls")
{
ExportExcelForm of = new ExportExcelForm();
of.mytable = tab;
if (of.mytable == null)
{
MessageBox.Show("没有导出数据。");
return;
}
of.savefimename = this.savefilename;
of.Show();
}
else if (sfd.FileName.Substring(sfd.FileName.LastIndexOf(".")).ToLower() == ".mdb" || sfd.FileName.Substring(sfd.FileName.LastIndexOf(".")).ToLower() == ".dbf")
{
ExportAccessForm of = new ExportAccessForm();
of.mytable = tab;
if (of.mytable == null)
{
MessageBox.Show("没有导出数据。");
return;
}
of.savefimename = this.savefilename;
of.Show();
}
}
catch (Exception W)
{
MessageBox.Show(W.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// 弹出导出窗口
/// </summary>
private void outToExcelByDataSource()
{
ExportExcelForm of = new ExportExcelForm();
of.mytable = (DataTable)this.datagv.DataSource;
if (of.mytable == null)
{
MessageBox.Show("没有导出数据。");
return;
}
of.savefimename = this.savefilename;
of.Show();
}
/// <summary>
/// 弹出导出窗口
/// </summary>
private void outToExcelByDataTable()
{
ExportExcelForm of = new ExportExcelForm();
DataTable dt = new DataTable();
for (int i = ; i < this.datagv.ColumnCount; i++)
{
dt.Columns.Add(this.datagv.Columns[i].HeaderText);
}
DataRow dr;
for (int i = ; i < this.datagv.RowCount; i++)
{
dr = dt.NewRow();
for (int j = ; j < this.datagv.ColumnCount; j++)
{
if (this.datagv.Columns[j].HeaderText == "sqlstring")
{
dr[j] = "";
}
else
{
if (this.datagv.Rows[i].Cells[j].Value == null)
{
dr[j] = "";
}
else
{
dr[j] = this.datagv.Rows[i].Cells[j].Value.ToString();
}
}
}
dt.Rows.Add(dr);
}
of.mytable = dt;
if (of.mytable == null)
{
MessageBox.Show("没有导出数据。");
return;
}
of.savefimename = this.savefilename;
of.Show();
}
/// <summary>
/// 弹出导出窗口
/// </summary>
private void outToAccessByDataSource()
{
if (this.savefilename.Substring(this.savefilename.LastIndexOf(".")).ToLower() == ".mdb")
{
ExportAccessForm of = new ExportAccessForm();
of.mytable = (DataTable)this.datagv.DataSource;
if (of.mytable == null)
{
MessageBox.Show("没有导出数据。");
return;
}
of.savefimename = this.savefilename;
of.Show();
}
else if (this.savefilename.Substring(this.savefilename.LastIndexOf(".")).ToLower() == ".dbf")
{
ExportDBFForm of = new ExportDBFForm();
of.mytable = (DataTable)this.datagv.DataSource;
if (of.mytable == null)
{
MessageBox.Show("没有导出数据。");
return;
}
of.savefimename = this.savefilename;
of.Show();
}
}
/// <summary>
/// 弹出导出窗口
/// </summary>
private void outToAccessByDataTable()
{
try
{
if (this.savefilename.Substring(this.savefilename.LastIndexOf(".")).ToLower() == ".mdb")
{
ExportAccessForm of = new ExportAccessForm();
DataTable dt = new DataTable();
for (int i = ; i < this.datagv.ColumnCount; i++)
{
dt.Columns.Add(this.datagv.Columns[i].HeaderText);
}
DataRow dr;
for (int i = ; i < this.datagv.RowCount; i++)
{
dr = dt.NewRow();
for (int j = ; j < this.datagv.ColumnCount; j++)
{
if (this.datagv.Columns[j].HeaderText == "sqlstring")
{
dr[j] = "";
}
else
{
if (this.datagv.Rows[i].Cells[j].Value == null)
{
dr[j] = "";
}
else
{
dr[j] = this.datagv.Rows[i].Cells[j].Value.ToString();
}
}
}
dt.Rows.Add(dr);
}
of.mytable = dt;
if (of.mytable == null)
{
MessageBox.Show("没有导出数据。");
return;
}
of.savefimename = this.savefilename;
of.Show();
}
else if (this.savefilename.Substring(this.savefilename.LastIndexOf(".")).ToLower() == ".dbf")
{
ExportDBFForm of = new ExportDBFForm();
DataTable dt = new DataTable();
for (int i = ; i < this.datagv.ColumnCount; i++)
{
dt.Columns.Add(this.datagv.Columns[i].HeaderText);
}
DataRow dr;
for (int i = ; i < this.datagv.RowCount; i++)
{
dr = dt.NewRow();
for (int j = ; j < this.datagv.ColumnCount; j++)
{
if (this.datagv.Columns[j].HeaderText == "sqlstring")
{
dr[j] = "";
}
else
{
if (this.datagv.Rows[i].Cells[j].Value == null)
{
dr[j] = "";
}
else
{
dr[j] = this.datagv.Rows[i].Cells[j].Value.ToString();
}
}
}
dt.Rows.Add(dr);
}
of.mytable = dt;
if (of.mytable == null)
{
MessageBox.Show("没有导出数据。");
return;
}
of.savefimename = this.savefilename;
of.Show();
}
}
catch
{
}
}