C#写一个从数据导出到Excel的表格
表名是Product
字段是u_Code(varchar(60)),u_Name(varchar(60)),Photo(图片数据类型image)
不要网页形式的我要C#窗口的那种容器是用的dataGridView1
要详细的代码,和步骤
网上拷贝的就不要了
表名是Product
字段是u_Code(varchar(60)),u_Name(varchar(60)),Photo(图片数据类型image)
不要网页形式的我要C#窗口的那种容器是用的dataGridView1
要详细的代码,和步骤
网上拷贝的就不要了
解决方案 »
- 多线程操作有错误,高手看看
- 大家在实际的项目中,使用 sqlcommandbuilder 自动生成的更新语句吗?感觉不如自已写好呀?
- 怎样为SpeechSynthesizer朗读文件设置进度条并且显示……
- 怎样解决大量字典表造成的sql语句困境
- 在有框架的页面中怎么在点一个框架中的数据时让另一个页面自动刷新呢
- 关于DropDownList的值的反写问题?
- 效率,效率问题。谁有经验的帮忙一下。有关缓存
- 用户控件为了能够即时释放资源,是不是应该重写Dispose()方法?
- 请教!
- C#调用函数的问题!在线等待,马上结分!
- 关于自定义表单的问题,很着急,在线忘高人指点。
- C# HttpWebRequest 使用WebProxy代理访问页面为什么访问量不增加
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.GridView1.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();
}
asp.net的 winfrom 没试过
/// 导出页面成Excel
/// </summary>
/// <param name="ctl"></param>
/// <param name="Charset"></param>
public void DGToExcel(System.Web.UI.Control ctl, string Charset)
{
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset = Charset == "" ? "UTF-8" : Charset;
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "application/ms-excel";
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
/// <summary>
/// 导出到Excel文档(说明一下这种导出Excel可以正常打开和编辑,但是其格式是文本格式)
/// </summary>
/// <param name="dtTable">导出的DataTable</param>
/// <param name="strExcelFilePath">Excel文件名</param>
/// <param name="bOverWrite">Excel文件名</param>
public static void ExportToExcelText(DataTable dtTable,string strExcelFilePath,bool bOverWrite)
{
try
{
FileInfo fi = new FileInfo(strExcelFilePath);
if(fi.Exists && !bOverWrite)
{
throw new Exception("同名文件已存在,无法重写!");
}
int i = 0;
int k = 0;
string strTemp = null;
StreamWriter sw = new StreamWriter(strExcelFilePath);
foreach(DataColumn dc in dtTable.Columns)
{
strTemp += dc.ColumnName + "\t";
}
sw.WriteLine(strTemp);
for(i = 0; i < dtTable.Rows.Count; i++)
{
strTemp = "";
for(k = 0; k < dtTable.Columns.Count; k++)
{
strTemp += string.Format("{0}\t",dtTable.Rows[i][k]);
}
sw.WriteLine(strTemp);
}
sw.Flush();
sw.Close();
}
catch(Exception me)
{
throw new Exception("写入Excel文档失败!" + me.Message);
}
}
网上有第三方类库,可以在不安装Excel的情况下导出Excel文件,很好很强大,我用过
private void BindData(string sql)
{
SqlConnection conn = new SqlConnection("uid=sa;pwd=123456;server=(local);database=myoffice;max pool size=10000;");
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds=new DataSet();
da.Fill(ds,"pers");
dataGridView1.DataSource = ds.Tables["pers"];
ds.Dispose();
conn.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
BindData("select *from pers");
} //导出数据到Excel
private void button3_Click(object sender, EventArgs e)
{
SaveAs();
} private void SaveAs() //另存新档按钮 导出成Excel
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "Export Excel File To";
saveFileDialog.ShowDialog();
Stream myStream;
myStream = saveFileDialog.OpenFile();
//StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
string str = "";
try
{
//写标题
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
if (i > 0)
{
str += "\t";
}
str += dataGridView1.Columns[i].HeaderText;
}
sw.WriteLine(str);
//写内容
for (int j = 0; j < dataGridView1.Rows.Count; j++)
{
string tempStr = "";
for (int k = 0; k < dataGridView1.Columns.Count; k++)
{
if (k > 0)
{
tempStr += "\t";
}
tempStr += dataGridView1.Rows[j].Cells[k].Value.ToString();
}
sw.WriteLine(tempStr);
}
sw.Close();
myStream.Close();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
finally
{
sw.Close();
myStream.Close();
}
}
using System.IO; // Stream等
using System.Data.SqlClient; // SqlConnection等
我对这方面不是很了解,链接地址也可以,最近等的很急,还有我的数据库账号是sa密码是没有的,数据库名UFdemo,服务器是 lanxinGG怎么写数据库的连接,
注意是在C#里不要ASP.NET的代码,是winfrom
我能雕饰成功并且能生成成功导出文件呢和导入文件的话,就给分甚至加分
http://download.csdn.net/source/2997557但你在dataGridView1上的改动要反映到DataTable里面去NPOI
http://www.cnblogs.com/tonyqus/category/182110.html
只要一行就搞定
这个东西,就只能使用excel处理了。
在C#中加载Office的dotnet类库。
在网上找一段代码写入没有问题吧。
带图片的excel导入和导出,让人无语....