Word,Excel编程小结
1 word
1.1采取域替换的方式。指定模板地址,从数据库读取值后替换域标记量即可,如下所示:
private string _templateName;
/// <summary>
/// 模板名称
/// </summary>
public string templateName
{
get { return _templateName; }
set { _templateName = value; }
}
strSQL = "select name from table where id=0 ";
dr = getReader(strSQL);
if (dr.Read())
{
_unit_demo = dr[ "name"].ToString();
}
dr.Close();
strSQL = strSQL.Replace("%id%", _id);
1.2采取html 转doc,替换标记量的方式,原理同上,只是最后的格式展现有些差异
1.3通用方式
Response.Charset = "GB2312";
Response.ContentType = "application/ms-download";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AddHeader("content-disposition", "attachment; filename=print" + DateTime.Now.ToShortDateString() + ".doc");
2 Excel
2.1编程,可以对整个表,单个cell操作,插入,删除,复制等操作,如下为
workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(sheetIndex);
foreach(DictionaryEntry dic in ht)
{
try
{
position = dic.Key.ToString();
rowIndex = Convert.ToInt32(position.Split(',')[0]);
columnIndex = Convert.ToInt32(position.Split(',')[1]);
workSheet.Cells[rowIndex,columnIndex] = dic.Value;
}
catch
{
this.KillExcelProcess();
throw new Exception("error!");
}
}
2.2邦定数据源控件(如gridview)
Response.Charset = "GB2312";
Response.ContentType = "application/vnd.ms-excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AddHeader("content-disposition", "attachment; filename=print" + DateTime.Now.ToShortDateString() + ".xls");
2.3采用宏方式
浙公网安备 33010602011771号