导出的代码就不多说了,下面 你可对要输入的字符串进行格式化带table 例如: // 获取要导出的字符串 // string data = string.Empty; data += "<table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\" border=\"1\">"; //写出列名 data += "<tr style=\"font-weight: bold; white-space: nowrap;\">"; data += "<td>" + "编号" + "</td>"; data += "<td>" + "卡号" + "</td>"; data += "</tr>"; //写出数据 foreach (CustomMade_CouponInfo coupon in couponList) { // 对编号进行格式化 string flag = string.Empty; if (coupon.CouponId.ToString().Length < 8) { for (int i = 0; i < (8 - coupon.CouponId.ToString().Length); i++) { flag += "0"; } } flag += coupon.CouponId.ToString(); data += "<tr>"; data += "<td style=\"vnd.ms-excel.numberformat:@\">" + flag + "</td>"; //这个地方的样式实际上就是让数值不会变成科学计数法 data += "<td style=\"vnd.ms-excel.numberformat:@\">" + coupon.ClaimCode + "</td>"; data += "</tr>"; } data += "</table>";
range = (Excel.Range)excel.Cells[rowIndex,colIndex];
range.NumberFormatLocal = "yyyy-mm-dd";
全过程如下,希望对你有用 public static void doExport(DataSet ds,string strExcelFileName) {
Excel.Application excel= new Excel.Application(); int rowIndex=1; int colIndex=0; excel.Application.Workbooks.Add(true); System.Data.DataTable table=ds.Tables[0] ; foreach(DataColumn col in table.Columns) { colIndex++; excel.Cells[1,colIndex]=col.ColumnName; } foreach(DataRow row in table.Rows) { rowIndex++; colIndex=0; Excel.Range range; foreach(DataColumn col in table.Columns) { colIndex++; string type = col.DataType.ToString(); range = (Excel.Range)excel.Cells[rowIndex,colIndex];
switch (type) { case "System.String": range.NumberFormatLocal = "@"; break; case "System.DateTime": range.NumberFormatLocal = "yyyy-mm-dd"; range.ColumnWidth = 10; break; } excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
你可对要输入的字符串进行格式化带table
例如:
// 获取要导出的字符串
//
string data = string.Empty;
data += "<table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\" border=\"1\">";
//写出列名
data += "<tr style=\"font-weight: bold; white-space: nowrap;\">";
data += "<td>" + "编号" + "</td>";
data += "<td>" + "卡号" + "</td>";
data += "</tr>";
//写出数据
foreach (CustomMade_CouponInfo coupon in couponList)
{
// 对编号进行格式化
string flag = string.Empty;
if (coupon.CouponId.ToString().Length < 8)
{
for (int i = 0; i < (8 - coupon.CouponId.ToString().Length); i++)
{
flag += "0";
}
}
flag += coupon.CouponId.ToString(); data += "<tr>";
data += "<td style=\"vnd.ms-excel.numberformat:@\">" + flag + "</td>"; //这个地方的样式实际上就是让数值不会变成科学计数法
data += "<td style=\"vnd.ms-excel.numberformat:@\">" + coupon.ClaimCode + "</td>";
data += "</tr>";
}
data += "</table>";
range.NumberFormatLocal = "yyyy-mm-dd";
{
Excel.Application excel= new Excel.Application();
int rowIndex=1;
int colIndex=0; excel.Application.Workbooks.Add(true);
System.Data.DataTable table=ds.Tables[0] ;
foreach(DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[1,colIndex]=col.ColumnName;
} foreach(DataRow row in table.Rows)
{
rowIndex++;
colIndex=0;
Excel.Range range;
foreach(DataColumn col in table.Columns)
{
colIndex++;
string type = col.DataType.ToString();
range = (Excel.Range)excel.Cells[rowIndex,colIndex];
switch (type)
{
case "System.String":
range.NumberFormatLocal = "@";
break;
case "System.DateTime":
range.NumberFormatLocal = "yyyy-mm-dd";
range.ColumnWidth = 10;
break;
}
excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
}
}
excel.Visible=false;
excel.ActiveWorkbook.SaveAs(strExcelFileName,Excel.XlFileFormat.xlExcel9795,null,null,false,false,Excel.XlSaveAsAccessMode.xlNoChange,null,null,null,null);
excel.Quit();
excel=null;
GC.Collect();
}