c# 中输出excel 我想在客户端输出excle怎么做,我会使用response在客户端输出csv文件,但是excle那样输出 的话,数据不能顺序写到对应的cell里。请大家告诉我一下。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不会把select a,b,c,d from tb改成你想要的顺序啊比如:select c,b,d,b from tb 不是这个意思,我想前台输出excle,数据怎么写到excle里。 控制Excel里的cell值就可以啊网上有很多代码的http://www.withub-soft.com/Show_IT.asp?id=225 你说的应该是编码问题不过直接输出html内容到一个文件改后缀名为.xls是个不错的办法 服务器生成之后,让客户端下载。http://www.360doc.com/content/07/0824/10/12027_691547.shtml public JExcelImportExport() { open_dlg.Title = "选择Excel文件"; open_dlg.Multiselect = false; open_dlg.Filter = "Excel文件(*.xls;*.xlsx)|*.xls;*.xlsx"; open_dlg.CheckPathExists = true; open_dlg.CheckFileExists = true; open_dlg.RestoreDirectory = true; save_dlg.Title = "另存为Excel文件"; save_dlg.Filter = "Excel2003文件(*.xls)|*.xls|Excel2007文件(*.xlsx)|*.xlsx"; save_dlg.CheckPathExists = true; save_dlg.RestoreDirectory = true; save_dlg.OverwritePrompt = true; } #region 从Excel导入数据 public DataSet ImportFromExcel() { DataSet ds = null; if(open_dlg.ShowDialog() == DialogResult.OK) { ds = ImportFromExcel(open_dlg.FileName); } return ds; } public DataSet ImportFromExcel(string file_path) { if(file_path == "") { return null; } connection = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + file_path + ";" + "Extended Properties=Excel 12.0"; OleDbDataAdapter excel_data = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connection); DataSet excel_ds = new DataSet(); try { excel_data.Fill(excel_ds); } catch(Exception ex) { MessageBox.Show(ex.ToString()); return null; } return excel_ds; } #endregion #region 数据导出到Excel public void ExportToExcel(DataSet ds) { if(save_dlg.ShowDialog() == DialogResult.OK) { ExportToExcel(ds, save_dlg.FileName); } } public void ExportToExcel(List<JCustomerGeomInfo> customer_geom_info_list) { if(save_dlg.ShowDialog() == DialogResult.OK) { ExportToExcel(customer_geom_info_list, save_dlg.FileName); } } public void ExportToExcel(DataSet ds, string file_store_path) { ApplicationClass excel = new ApplicationClass(); int row_index = 1; int col_index = 1; excel.Application.Workbooks.Add(true); System.Data.DataTable dt = ds.Tables[0]; foreach(DataColumn dc in dt.Columns) { excel.Cells[row_index, col_index] = dc.ColumnName; col_index++; } foreach(DataRow dr in dt.Rows) { row_index++; col_index = 1; foreach(DataColumn dc in dt.Columns) { excel.Cells[row_index, col_index] = dr[dc.ColumnName].ToString(); col_index++; } } excel.Visible = false; try { excel.ActiveWorkbook.SaveAs(file_store_path, XlFileFormat.xlWorkbookDefault, null, null, false, false, XlSaveAsAccessMode.xlNoChange, null, null, null, null, null); } catch(Exception ex) { MessageBox.Show(ex.ToString()); } finally { excel.Quit(); GC.Collect(); } }我以前写的excel导入导出 你看看 经纬度字符串寻求更好的转换方式 关于DataGridView绑定后还能不能改列的类型 关于 ListBox.SelectedItem.Value问题 C#创建控制台应用程序,如何连接数据库呢??在线等 实现读取文本内容 很容易的问题,但我总报错,帮帮我吧?在线等 HttpWebResponse的奇怪问题 要做个打字记时的东西希望大家帮忙进来看看 有兴趣的请进入 C#:关于指纹识别SDK,VS能正常调用,但服务器上却不行 问个google上的网页控件 将dataGridView中显示的数据打印出来
改成你想要的顺序啊
比如:select c,b,d,b from tb
网上有很多代码的
http://www.withub-soft.com/Show_IT.asp?id=225
http://www.360doc.com/content/07/0824/10/12027_691547.shtml
{
open_dlg.Title = "选择Excel文件";
open_dlg.Multiselect = false;
open_dlg.Filter = "Excel文件(*.xls;*.xlsx)|*.xls;*.xlsx";
open_dlg.CheckPathExists = true;
open_dlg.CheckFileExists = true;
open_dlg.RestoreDirectory = true; save_dlg.Title = "另存为Excel文件";
save_dlg.Filter = "Excel2003文件(*.xls)|*.xls|Excel2007文件(*.xlsx)|*.xlsx";
save_dlg.CheckPathExists = true;
save_dlg.RestoreDirectory = true;
save_dlg.OverwritePrompt = true;
} #region 从Excel导入数据 public DataSet ImportFromExcel()
{
DataSet ds = null; if(open_dlg.ShowDialog() == DialogResult.OK)
{
ds = ImportFromExcel(open_dlg.FileName);
} return ds;
} public DataSet ImportFromExcel(string file_path)
{
if(file_path == "")
{
return null;
} connection = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + file_path + ";" +
"Extended Properties=Excel 12.0";
OleDbDataAdapter excel_data = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connection); DataSet excel_ds = new DataSet(); try
{
excel_data.Fill(excel_ds);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
return null;
} return excel_ds;
} #endregion #region 数据导出到Excel public void ExportToExcel(DataSet ds)
{
if(save_dlg.ShowDialog() == DialogResult.OK)
{
ExportToExcel(ds, save_dlg.FileName);
}
} public void ExportToExcel(List<JCustomerGeomInfo> customer_geom_info_list)
{
if(save_dlg.ShowDialog() == DialogResult.OK)
{
ExportToExcel(customer_geom_info_list, save_dlg.FileName);
}
} public void ExportToExcel(DataSet ds, string file_store_path)
{
ApplicationClass excel = new ApplicationClass(); int row_index = 1;
int col_index = 1; excel.Application.Workbooks.Add(true); System.Data.DataTable dt = ds.Tables[0];
foreach(DataColumn dc in dt.Columns)
{
excel.Cells[row_index, col_index] = dc.ColumnName;
col_index++;
} foreach(DataRow dr in dt.Rows)
{
row_index++;
col_index = 1; foreach(DataColumn dc in dt.Columns)
{
excel.Cells[row_index, col_index] = dr[dc.ColumnName].ToString();
col_index++;
}
} excel.Visible = false; try
{
excel.ActiveWorkbook.SaveAs(file_store_path, XlFileFormat.xlWorkbookDefault, null, null, false,
false,
XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
excel.Quit();
GC.Collect();
}
}
我以前写的excel导入导出 你看看