반응형
ds = JsonConvert.DeserializeObject(srtResult);

//strJson = value.vJsonData.ToString(); //Object로 보내기 위한 작업
//여기서 부터 엑셀 데이터 만들기. (Ds)
//ds = JsonConvert.DeserializeObject(value.vJsonData);

Excel.Application ap = new Excel.Application();
Excel.Workbook excelWorkBook = ap.Workbooks.Add();

foreach (DataTable Excel_dt in ds.Tables)
{
	Excel.Worksheet ws = excelWorkBook.Sheets.Add();
	ws.Name = Excel_dt.TableName;

	for(int columnHeaderIndex = 1; columnHeaderIndex <= Excel_dt.Columns.Count; columnHeaderIndex++)
	{
		ws.Cells[1, columnHeaderIndex] = Excel_dt.Columns[columnHeaderIndex -1].ColumnName;
		ws.Cells[1, columnHeaderIndex].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightSteelBlue);
	}

	for(int rowIndex = 0; rowIndex < Excel_dt.Rows.Count; rowIndex++)
	{
		for(int columnIndex = 0; columnIndex < Excel_dt.Columns.Count; columnIndex++)
		{
			ws.Cells[rowIndex + 2 , columnIndex + 1] = Excel_dt.Rows[rowIndex].ItemArray[columnIndex].ToString();
		}
	}
	ws.Columns.AutoFit();
}

//파일저장
string strDateTime = strDateTime = DateTime.Now.ToString("yyyyMMddHHmmssFFF");
string strPath = "Files/";
//System.IO.FileInfo fi;
//fi = new System.IO.FileInfo(Server.MapPath("~/Files/")+strDateTime+".xlsx");

excelWorkBook.SaveAs(Server.MapPath("~/"+strPath)+strDateTime+".xlsx",Excel.XlFileFormat.xlOpenXMLWorkbook);
excelWorkBook.Close(true);
ap.Quit();

================================필독 ========================================

- import excel이 필요합니다. office12에 있습니다.

- 데이터 많으면 굉장히 느립니다. 

더 빠른 방법 참고 https://1061025.tistory.com/54

 

 

 

+ Recent posts