다음 코드를 사용하여 C#에서 Excel로 데이터 집합을 내보내는 중입니다. 나는 다음과 같은 출력을 얻고있다 :내보낼 때 출력 수정
ABC | 1 | 2/15/2011 21:36 | Systems Analyst | Project Duration
ABC | 1 | 2/15/2011 21:36 | Systems Analyst | Skill of Team
ABC | 1 | 2/15/2011 21:36 | Systems Analyst | Process
내가하고 싶은 무엇을, 그러나, 다음과 같은 형식으로 출력 데이터이며, 열 머리글을 추가합니다. StoryCategoryID에 대한 출력이 1이면 NEGATIVE, 출력이 0이면 POSITIVE를 표시하고 싶습니다. 도와주세요!
Story | Story Type | Date | Project Member | Tag 1 | Tag 2 | Tag 3
ABC | Negative | 2/15/2011 21:36 | Project Duration | Skill of Team | Process
코드 : 모든
protected void btnExcelExport_Click(object sender, EventArgs e)
{
string sql = null;
string data = null;
//string path = save_as.Text;
int i = 0;
int j = 0;
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
//connectionString = "data source=servername;initial catalog=databasename;user id=username;password=password;";
SqlConnection cnn = new SqlConnection(GetConnectionString());
cnn.Open();
sql = "SELECT s.Story, s.StoryCategoryID, s.CreationDate, m.CompanyRole, af.Name FROM Story s INNER JOIN ProjectIterationMember pm ON pm.ProjectIterationMemberID = s.ProjectIterationMemberID INNER JOIN Iterations i ON i.ProjectIterationID = pm.ProjectIterationID INNER JOIN Member m ON m.MemberID = pm.MemberID INNER JOIN ProjectStoryFactors psf ON psf.StoryID = s.StoryID INNER JOIN AgileFactors af ON af.AgileFactorID = psf.AgileFactorID WHERE i.ProjectID = '" + proj_id + "'";
SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
DataSet ds = new DataSet();
dscmd.Fill(ds);
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
{
data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
xlWorkSheet.Cells[i + 1, j + 1] = data;
}
}
xlWorkBook.SaveAs("excelDocument" + DateTime.Now.Ticks + ".xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
//MessageBox.Show("Excel file created , you can find the file c:\\csharp.net-informations.xls");
}
직접적인 해결책은 아니지만 http://excelpackage.codeplex.com/에서 더 쉽게 작업 할 수 있습니다. COM 종속성이 없으며 형식 지정과 관련하여 매우 유연합니다. –