Click here to Skip to main content
14,304,321 members
Rate this:
Please Sign up or sign in to vote.
See more:
pls give the content type for open a xlsx file in broowser....

i alredy used
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
but it is not useful...i cant open the xlsx file...



pls help me
Posted
Updated 29-Mar-19 4:55am
Rate this:
Please Sign up or sign in to vote.

Solution 1

The content type for .xlsx files is:
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

Or use this:
Response.ContentType = "application/vnd.ms-excel";

Response.AppendHeader("content-disposition", "attachment; filename=myfile.xls");

For Excel 2007 and above the MIME type differs
Response.ContentType = "application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

Response.AppendHeader("content-disposition", "attachment; filename=myfile.xlsx");

Or if you are trying to read the file then try this:

DataSet objds = new DataSet();
string ConnStr = "";
if (FileExtension == ".xlsx")
{
    ConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=No;IMEX=1\";";
}
else
{
    ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\";";

}
OleDbCommand selectCommand = new OleDbCommand();
OleDbConnection connection = new OleDbConnection();
OleDbDataAdapter adapter = new OleDbDataAdapter();
connection.ConnectionString = ConnStr;
string strSQL = "SELECT * FROM [Sheet1$]";
if (connection.State != ConnectionState.Open)
    connection.Open();
OleDbCommand cmd = new OleDbCommand(strSQL, connection);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(objds);
connection.Close();


All the best.
--Amit
   
v2
Comments
__PP__ 22-Oct-12 2:09am
   
sorry it does not work...I got error like excel cannot open the file format...
_Amy 22-Oct-12 2:18am
   
Can you post your code here?
_Amy 22-Oct-12 2:24am
   
Try my updated answer.
__PP__ 22-Oct-12 2:31am
   
DataTable dt = new DataTable();
var Inputs = ProjectAttributeMasterService.GetInputAttributes(7);
Response.AddHeader("content-disposition", "attachment;filename=Report.xlsx");
Response.Charset = "";

Response.ContentType = "application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

I want to open that excel file and that file contain the values of list(Inputs)...
for that i using the folowing code..but dont know how connect with the xlsx file


Excel.Application excel = new Excel.Application();
Excel.Workbook wb = excel.Application.Workbooks.Add(true);
Excel.Worksheet excelSheet = (Excel.Worksheet)excel.ActiveSheet;


int col = 1;
for (int i = 0; i < Inputs.Count; i++) // Loop through List with for

{
excelSheet.Cells[1, col] = Inputs[i].ToString();
col++;

}
Rate this:
Please Sign up or sign in to vote.

Solution 2

hi,
I am also Facing Same Problem but I got some alternator If It can help u

(1)Check This One on stackoverflow

http://stackoverflow.com/questions/13724988/unable-to-generate-the-xlsx-file

2)always use
Response.ContentType = "application/vnd.ms-excel";

Just Change the File extention from xlsx to xls
and open it it will work
   
Comments
SALIM MANDREKAR 19-Aug-14 2:15am
   
its not working
SALIM MANDREKAR 19-Aug-14 2:16am
   
GridView2.DataSource = dt;
GridView2.DataBind();
DirectoryInfo dir = new DirectoryInfo(@"C:\Reports");
dir.Create();
StringBuilder Excelfile = new StringBuilder("ExcelReport(");
//Excelfile.Append(DateTime.Now.ToString("MMM dd, yyyy "));
Excelfile.Append(DateTime.Now.ToString("MMM dd,HH.mm"));
Excelfile.Append(").xlsx");
FileInfo file = new FileInfo(@"C:\Reports\" + Excelfile);//C:\List Data\
StreamWriter streamWriter = file.CreateText();

StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
GridView2.RenderControl(htmlTextWriter);
streamWriter.Write(stringWriter.ToString());

htmlTextWriter.Close();
streamWriter.Close();
stringWriter.Close();
Session.Remove("TempCatering");
byte[] Content = File.ReadAllBytes(@"C:\New List Reports\" + file.Name);
Response.ContentType ="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.SetCookie(new HttpCookie("token", Request.Form["token"]));
Response.AddHeader("content-disposition", "attachment; filename=" + file.Name);
Response.BufferOutput = true;
Response.OutputStream.Write(Content, 0, Content.Length);
Response.End();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100