excel导入和导出续

因项目发布到windows server 2008有权限问题,经多方设置未起效果

故引用下面方法实现功能

#region 保存在本地
202 /// <summary>
203 /// 保存在本地
204 /// </summary>
205 /// <param name="path">绝对路径</param>
206 /// <param name="titles">Excel标题</param>
207 /// <param name="ds">数据</param>
208   private static void SaveToXls(string path, IList<String> titles, DataSet ds)
209 {
210 System.Data.DataTable dt = ds.Tables[0];
211 ExcelFile excelFile = new ExcelFile();
212 ExcelWorksheet sheet = excelFile.Worksheets.Add("Sheet1");
213
214
215 int columns = dt.Columns.Count;
216 int rows = dt.Rows.Count;
217 int index = 0;
218 if (titles != null && titles.Count > 0)
219 {
220 for (int i = 0; i < titles.Count; i++)
221 {
222
223 sheet.Cells[0, i].Value = titles[i];
224 }
225 index++;
226 }
227
228 for (int i = 0; i < rows; i++)
229 {
230 for (int j = 0; j < columns; j++)
231 {
232 sheet.Cells[i + index, j].Value = dt.Rows[i][j].ToString();
233 }
234 }
235 excelFile.SaveXls(path);
236
237 }
238 #endregion

posted @ 2011-06-10 10:03  Jake.Xu  阅读(141)  评论(0编辑  收藏  举报