반응형
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

 

 

 

반응형
[HttpPost]
public ActionResult GetRSSData(JsonData value)
{
	DataTable dt = new DataTable();
	dt = JsonConvert.DeserializeObject(value.vJsonData);
	string str_RssData = "";

	XmlDocument rssXmlDoc = new XmlDocument();
	rssXmlDoc.Load(dt.Rows[0]["Blog_URL"].ToString());

	str_RssData = rssXmlDoc.InnerXml;

	//XmlNodeList rssNodes = rssXmlDoc.SelectNodes("rss/channel/item");
	//StringBuilder rssContent = new StringBuilder();

	return this.Content(str_RssData, "text/xml");
}

 

반응형
[HttpGet]
public ActionResult GetCargoInfo(RtnFilesInfo value)
{
string str_crkyCn = value.crkyCn;
string str_cargMtNo = value.cargMtNo;
string str_mblNo = value.mblNo;
string str_hblNo = value.hblNo;
string str_blYy = value.blYy;

//화물관리번호 , BL 사용해서 했는지 확인 여부 필요.

	try
	{
		string url = "https://unipass.customs.go.kr:38010/ext/rest/cargCsclPrgsInfoQry/retrieveCargCsclPrgsInfo?crkyCn=i220k129u161i054w030p040s2&cargMtNo=00ANLU083N59007001";
		//uri = new Uri(url); // string 을 Uri 로 형변환
		string responseText = string.Empty;

		HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
		request.Method = "GET";

		using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
		{
			HttpStatusCode status = resp.StatusCode;
			Console.WriteLine(status);  // 정상이면 "OK"


			Stream respStream = resp.GetResponseStream();
			using (StreamReader sr = new StreamReader(respStream))
			{
				responseText = sr.ReadToEnd();
				Console.WriteLine(responseText);
			}
		}
	}
	catch(Exception ex)
	{
		Console.WriteLine(ex.Message);
	}

	string sURL;
	sURL = "https://unipass.customs.go.kr:38010/ext/rest/cargCsclPrgsInfoQry/retrieveCargCsclPrgsInfo?crkyCn=i220k129u161i054w030p040s2&cargMtNo=00ANLU083N59007001";

	WebRequest wrGETURL;
	wrGETURL = WebRequest.Create(sURL);
	WebProxy myProxy = new WebProxy("myproxy", 80);

	myProxy.BypassProxyOnLocal = true;

	wrGETURL.Proxy = WebProxy.GetDefaultProxy();

	Stream objStream;
	objStream = wrGETURL.GetResponse().GetResponseStream();

	StreamReader objReader = new StreamReader(objStream);

	string sLine = "";
	int i = 0;
	string ReadingText = "";
	while (sLine != null)
	{
		i++;
		sLine = objReader.ReadLine();
		ReadingText += sLine;
		//if (sLine != null)
		//    Console.WriteLine("{0}:{1}", i, sLine);
	}
	//MessageBox.Show(ReadingText);
	//Console.WriteLine(ReadingText);
				//Console.ReadLine();
	System.Diagnostics.Debug.WriteLine(ReadingText);

	return this.Content(ReadingText, "text/xml");
}
반응형

[2020.04.23]
저는 유니패스에서 관세청 정보를 가져와서 사용하기 위해서 API를 확인하는 도중
유니패스 관세청 정보는 URL을 보내 XML로 데이터를 받는 방법으로 주기 때문에 XML을 텍스트로 가공하여 웹에 뿌려주는 로직을 만들기 위해서
아래와 같은 방법으로 C#에 코딩하여 사용 하였었습니다.

URL을 던져서 데이터를 받아와 XML를 한줄로 만드는 로직 입니다.

 

string sURL;
sURL = ""; //XML을 가져올 URL을 적어줍니다.
 
WebRequest wrGETURL;
wrGETURL = WebRequest.Create(sURL);

WebProxy myProxy = new WebProxy("myproxy", 80); //프록시 세팅
myProxy.BypassProxyOnLocal = true;
wrGETURL.Proxy = WebProxy.GetDefaultProxy();

Stream objStream;
objStream = wrGETURL.GetResponse().GetResponseStream();
StreamReader objReader = new StreamReader(objStream); //XML 데이터 가져오기

//XML 데이터 가져와서 string 한줄로 정리하는 로직
string sLine = "";
int i = 0;
string ReadingText = "";
while (sLine != null)
{
	i++;
	sLine = objReader.ReadLine();
	ReadingText += sLine;	
}

//제대로 XML이 생성 됐는지 확인 하는 명령어
System.Diagnostics.Debug.WriteLine(ReadingText);

return this.Content(ReadingText, "text/xml");
반응형

[2021.04.23]
데이터 채번을 하기 위해서 랜덤으로 텍스트 및 숫자를 가져올 수 있게 하였습니다.
매개변수로 numLength는 원하는 길이 만큼 숫자를 가져올 수 있습니다.

public string fnGetRandomString(int numLength){

	string  strResult = "";
	Random rand = new Random();
	string strRandomChar = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789"; //랜덤으로 들어갈 문자 및 숫자 
	
	StringBuilder rs = new StringBuilder();  
	
	//매개변수로 받은 numLength만큼 데이터를 가져 올 수 있습니다.
	for(int i=0; i<numLength; i++){
		rs.Append(strRandomChar[(int)(rand.NextDouble() * strRandomChar.Length)]);  
	}
	strResult = rs.ToString();
	
	return strResult;
}
반응형

[2021.05.07]

Console.WriteLine으로 데이터가 보이지 않을 때 아래와 같은 문구를 사용하면

Debug 창에서 볼 수 있습니다.

System.Diagnostics.Debug.WriteLine(결과값);

 

+ Recent posts