C#导出excel格式问题

gtwfearth 2011-08-18 04:21:15
我用.net导出excel如何使身份证号码导出来的时候为文本格式啊,我导出后excel会用科学计数法显示身份证号码,如果我在身份证号码前面加 ' ,在显示的时候会将 '显示出来,当我一个单元格一个单元格点击后,'才会消失,并且会用文本格式存储身份证号码,我想问下有没有好的办法使导出的身份证号码为文本格式存储,出生日期为时间格式啊,谢谢大家
...全文
682 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
行侠者 2013-07-31
  • 打赏
  • 举报
回复
我之前做过C#导出Excel,还可以导出图形到Excel内 http://download.csdn.net/detail/jx_521/5819725 可以指定导出哪些列、可以设置Excel列宽、标题名、列名 我是用aspose.cells.dll做的,希望可以帮助你。
学习鬼谷子 2012-10-31
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]
我的代码是这样的
该如何修改啊
protected void Button2_Click(object sender, EventArgs e)
{
DataTable dt11 = new DataTable();
using (SqlConnection con = new SqlConnection
ConfigurationManager.ConnectionSt……
[/Quote]
我代码也是这样的,直接从数据库表导出到Excel中,强大的Excel把我的带“-”的数据处理成日期型的,将带“.”的数据处理成浮点型的,同9楼,求在哪设置Excel格式成文本形式的输出????
md5e 2011-08-19
  • 打赏
  • 举报
回复
现在喜欢捡现成的人太多了,满足你吧下载Galsun.Common

Server.MapPath("/ExcelTemplate/reg.xls");//reg为模板,设置表头等信息,你也可不用这做作法

DataSet ds = db.GetDataSet(query);

if (ds != null)
{
string _template = Server.MapPath("/ExcelTemplate/reg.xls");
System.Random ran = new Random();
string _strFileName = string.Format("{0}_{1}.xls", DateTime.Now.ToString("yyyyMMddHHmmss"), ran.Next(9999));
Galsun.Common.NOPIHelper.ExportByWeb(ds.Tables[0], _template, 3, _strFileName);
}
gtwfearth 2011-08-19
  • 打赏
  • 举报
回复
我的代码是这样的
该如何修改啊
protected void Button2_Click(object sender, EventArgs e)
{
DataTable dt11 = new DataTable();
using (SqlConnection con = new SqlConnection
ConfigurationManager.ConnectionStrings["connet"].ConnectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from renyuanjibenxinxibiao", con);
try
{
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt11);

}
catch
{
Response.Write("没有任何内容!");
}
con.Close();
}

//DataTable dt = ds.Tables["table1"];
StringWriter sw = new StringWriter();
sw.WriteLine("工号\t姓名\t身份证号码\t出生年月");
foreach (DataRow dr in dt11.Rows)
{
sw.WriteLine(dr["gonghao"].ToString().ToString() + "\t" + dr["xingming"].ToString() + "\t" + dr["shenfenzhenghaoma"].ToString() + "\t" + dr["chushengnianyue"].ToString());
}
sw.Close();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
//Excel.Range myrange =

Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("基本信息表.xls", Encoding.UTF8).ToString());
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");


Response.Write(sw.ToString());
Response.End();
}


我该在什么地方设置excel的格式,应该怎么修改啊,谢谢啊,我新手,很多都不会,晚了领导就该批斗了。。。。。。。。。。。。。。。,谢谢大家
pwxcomer 2011-08-18
  • 打赏
  • 举报
回复

private void button1_Click(object sender, EventArgs e)
{
ExportToExcel(@"d:/abcd.xls");
}
public static void ExportToExcel(string FileSavePath)
{
Excel.Application excel = null;
Excel._Workbook xBk = null;
Excel._Worksheet xSt = null;
try
{
excel = new Excel.ApplicationClass();
xBk = excel.Workbooks.Add(true);

//创建一个Sheet
if (null == xSt)
{
xSt = (Excel._Worksheet)xBk.Worksheets.Add(Type.Missing, Type.Missing, 1, Type.Missing);
}
else
{
xSt = (Excel._Worksheet)xBk.Worksheets.Add(Type.Missing, xSt, 1, Type.Missing);
}
//设置SheetName
xSt.Name = "Excel";


string str = "需要显示的身份证号码";
xSt.get_Range(xSt.Cells[1, 1], xSt.Cells[1, 1]).NumberFormatLocal = "@";
xSt.Cells[ 1, 1] = str;

xSt.Columns.AutoFit();

//删除Sheet1
excel.DisplayAlerts = false; //注意一定要加上这句
((Microsoft.Office.Interop.Excel.Worksheet)xBk.Worksheets["Sheet1"]).Delete();
excel.DisplayAlerts = true;//注意一定要加上这句

object objOpt = System.Reflection.Missing.Value;
excel.Visible = false;

xBk.SaveCopyAs(FileSavePath);
xBk.Close(false, null, null);
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
xBk = null;
excel = null;
xSt = null;
GC.Collect();

}
catch (Exception e)
{
xBk.Close(false, null, null);
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
xBk = null;
excel = null;
xSt = null;
GC.Collect();
}
}
gtwfearth 2011-08-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 pwxcomer 的回复:]
C# code

string str = "需要显示的身份证号码";
xSt.get_Range(xSt.Cells[1, 1], xSt.Cells[1, 1]).NumberFormatLocal = "@";//设置单元格格式为文本
xSt.Cells[ 1, 1] = str;
xSt.Columns.AutoFit();


htt……
[/Quote]

xst是什么东东啊?
pwxcomer 2011-08-18
  • 打赏
  • 举报
回复

string str = "需要显示的身份证号码";
xSt.get_Range(xSt.Cells[1, 1], xSt.Cells[1, 1]).NumberFormatLocal = "@";//设置单元格格式为文本
xSt.Cells[ 1, 1] = str;
xSt.Columns.AutoFit();

http://blog.csdn.net/heliangpk/article/details/4405915
gtwfearth 2011-08-18
  • 打赏
  • 举报
回复
我仅仅想从datatabel中导出到excel,你给我说的老帖中找了好几个都没找到合适的。。。。。
md5e 2011-08-18
  • 打赏
  • 举报
回复
NPOI吧,更多


157 #region 填充内容
158 HSSFRow dataRow = sheet.CreateRow(rowIndex);
159 foreach (DataColumn column in dtSource.Columns)
160 {
161 HSSFCell newCell = dataRow.CreateCell(column.Ordinal);
162
163 string drValue = row[column].ToString();
164
165 switch (column.DataType.ToString())
166 {
167 case "System.String"://字符串类型
168 newCell.SetCellValue(drValue);
169 break;
170 case "System.DateTime"://日期类型
171 DateTime dateV;
172 DateTime.TryParse(drValue, out dateV);
173 newCell.SetCellValue(dateV);
174
175 newCell.CellStyle = dateStyle;//格式化显示
176 break;
177 case "System.Boolean"://布尔型
178 bool boolV = false;
179 bool.TryParse(drValue, out boolV);
180 newCell.SetCellValue(boolV);
181 break;
182 case "System.Int16"://整型
183 case "System.Int32":
184 case "System.Int64":
185 case "System.Byte":
186 int intV = 0;
187 int.TryParse(drValue, out intV);
188 newCell.SetCellValue(intV);
189 break;
190 case "System.Decimal"://浮点型
191 case "System.Double":
192 double doubV = 0;
193 double.TryParse(drValue, out doubV);
194 newCell.SetCellValue(doubV);
195 break;
196 case "System.DBNull"://空值处理
197 newCell.SetCellValue("");
198 break;
199 default:
200 newCell.SetCellValue("");
201 break;
202 }
203
204 }
205 #endregion

peaker2009 2011-08-18
  • 打赏
  • 举报
回复
http://www.cnblogs.com/springyangwc/archive/2011/08/12/2136498.html
子夜__ 2011-08-18
  • 打赏
  • 举报
回复

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧